Skip to content

Commit

Permalink
汐洛绞架安卓web选择框点击无响应 #814
Browse files Browse the repository at this point in the history
  • Loading branch information
Soltus committed Jun 10, 2024
1 parent c618571 commit ac507de
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 139 deletions.
254 changes: 128 additions & 126 deletions app/src/dialog/index.ts
Original file line number Diff line number Diff line change
@@ -1,126 +1,128 @@
import {genUUID} from "../util/genID";
/// #if !MOBILE
import {moveResize} from "./moveResize";
/// #endif
import {isMobile} from "../util/functions";
import {isNotCtrl} from "../protyle/util/compatibility";
import type {Protyle} from "../protyle";
import {Constants} from "../constants";

export class Dialog {
private destroyCallback: (options?: IObject) => void;
public element: HTMLElement;
private id: string;
private disableClose: boolean;
public editors: { [key: string]: Protyle };
public data: any;

constructor(options: {
positionId?: string,
title?: string,
transparent?: boolean,
content: string,
width?: string,
height?: string,
destroyCallback?: (options?: IObject) => void,
disableClose?: boolean,
hideCloseIcon?: boolean,
disableAnimation?: boolean,
resizeCallback?: (type: string) => void
}) {
this.disableClose = options.disableClose;
this.id = genUUID();
window.siyuan.dialogs.push(this);
this.destroyCallback = options.destroyCallback;
this.element = document.createElement("div") as HTMLElement;
let left;
let top;
if (!isMobile() && options.positionId) {
const dialogPosition = window.siyuan.storage[Constants.LOCAL_DIALOGPOSITION][options.positionId];
if (dialogPosition) {
if (dialogPosition.left + dialogPosition.width + 34 <= window.innerWidth &&
dialogPosition.top + dialogPosition.height <= window.innerHeight) {
left = dialogPosition.left + "px";
top = dialogPosition.top + "px";
options.width = dialogPosition.width + "px";
options.height = dialogPosition.height + "px";
}
}
}
this.element.innerHTML = `<div class="b3-dialog" style="z-index: ${++window.siyuan.zIndex};${typeof left === "string" ? "display:block" : ""}">
<div class="b3-dialog__scrim"${options.transparent ? 'style="background-color:transparent"' : ""}></div>
<div class="b3-dialog__container" style="width:${options.width || "auto"};height:${options.height || "auto"};left:${left};top:${top}">
<svg ${(isMobile() && options.title) ? 'style="top:0;right:0;"' : ""} class="b3-dialog__close${(this.disableClose || options.hideCloseIcon) ? " fn__none" : ""}"><use xlink:href="#iconCloseRound"></use></svg>
<div class="resize__move b3-dialog__header${options.title ? "" : " fn__none"}" onselectstart="return false;">${options.title || ""}</div>
<div class="b3-dialog__body">${options.content}</div>
<div class="resize__rd"></div><div class="resize__ld"></div><div class="resize__lt"></div><div class="resize__rt"></div><div class="resize__r"></div><div class="resize__d"></div><div class="resize__t"></div><div class="resize__l"></div>
</div></div>`;

this.element.querySelector(".b3-dialog__scrim").addEventListener("click", (event) => {
if (!this.disableClose) {
this.destroy();
}
event.preventDefault();
event.stopPropagation();
});
if (!this.disableClose) {
this.element.querySelector(".b3-dialog__close").addEventListener("click", (event) => {
this.destroy();
event.preventDefault();
event.stopPropagation();
});
}
document.body.append(this.element);
if (options.disableAnimation) {
this.element.classList.add("b3-dialog--open");
} else {
setTimeout(() => {
this.element.classList.add("b3-dialog--open");
});
}
/// #if !MOBILE
moveResize(this.element.querySelector(".b3-dialog__container"), options.resizeCallback);
/// #endif
}

public destroy(options?: IObject) {
// av 修改列头emoji后点击关闭emoji图标
if ((this.element.querySelector(".b3-dialog") as HTMLElement).style.zIndex < window.siyuan.menus.menu.element.style.zIndex) {
// https://github.com/siyuan-note/siyuan/issues/6783
window.siyuan.menus.menu.remove();
}
this.element.remove();
if (this.destroyCallback) {
this.destroyCallback(options);
}
window.siyuan.dialogs.find((item, index) => {
if (item.id === this.id) {
window.siyuan.dialogs.splice(index, 1);
return true;
}
});
// https://github.com/siyuan-note/siyuan/issues/10475
document.getElementById("drag")?.classList.remove("fn__hidden");
}

public bindInput(inputElement: HTMLInputElement | HTMLTextAreaElement, enterEvent?: () => void, bindEnter = true) {
inputElement.focus();
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
if (event.isComposing) {
event.preventDefault();
return;
}
if (event.key === "Escape") {
this.destroy();
event.preventDefault();
event.stopPropagation();
return;
}
if (!event.shiftKey && isNotCtrl(event) && event.key === "Enter" && enterEvent && bindEnter) {
enterEvent();
event.preventDefault();
event.stopPropagation();
}
});
}
}
import {genUUID} from "../util/genID";
/// #if !MOBILE
import {moveResize} from "./moveResize";
/// #endif
import {isMobile} from "../util/functions";
import {isNotCtrl} from "../protyle/util/compatibility";
import type {Protyle} from "../protyle";
import {Constants} from "../constants";
import { unmountReactRootsArray } from "../sillot/util/react";

export class Dialog {
private destroyCallback: (options?: IObject) => void;
public element: HTMLElement;
private id: string;
private disableClose: boolean;
public editors: { [key: string]: Protyle };
public data: any;

constructor(options: {
positionId?: string,
title?: string,
transparent?: boolean,
content: string,
width?: string,
height?: string,
destroyCallback?: (options?: IObject) => void,
disableClose?: boolean,
hideCloseIcon?: boolean,
disableAnimation?: boolean,
resizeCallback?: (type: string) => void
}) {
this.disableClose = options.disableClose;
this.id = genUUID();
window.siyuan.dialogs.push(this);
this.destroyCallback = options.destroyCallback;
this.element = document.createElement("div") as HTMLElement;
let left;
let top;
if (!isMobile() && options.positionId) {
const dialogPosition = window.siyuan.storage[Constants.LOCAL_DIALOGPOSITION][options.positionId];
if (dialogPosition) {
if (dialogPosition.left + dialogPosition.width + 34 <= window.innerWidth &&
dialogPosition.top + dialogPosition.height <= window.innerHeight) {
left = dialogPosition.left + "px";
top = dialogPosition.top + "px";
options.width = dialogPosition.width + "px";
options.height = dialogPosition.height + "px";
}
}
}
this.element.innerHTML = `<div class="b3-dialog" style="z-index: ${++window.siyuan.zIndex};${typeof left === "string" ? "display:block" : ""}">
<div class="b3-dialog__scrim"${options.transparent ? 'style="background-color:transparent"' : ""}></div>
<div class="b3-dialog__container" style="width:${options.width || "auto"};height:${options.height || "auto"};left:${left};top:${top}">
<svg ${(isMobile() && options.title) ? 'style="top:0;right:0;"' : ""} class="b3-dialog__close${(this.disableClose || options.hideCloseIcon) ? " fn__none" : ""}"><use xlink:href="#iconCloseRound"></use></svg>
<div class="resize__move b3-dialog__header${options.title ? "" : " fn__none"}" onselectstart="return false;">${options.title || ""}</div>
<div class="b3-dialog__body">${options.content}</div>
<div class="resize__rd"></div><div class="resize__ld"></div><div class="resize__lt"></div><div class="resize__rt"></div><div class="resize__r"></div><div class="resize__d"></div><div class="resize__t"></div><div class="resize__l"></div>
</div></div>`;

this.element.querySelector(".b3-dialog__scrim").addEventListener("click", (event) => {
if (!this.disableClose) {
this.destroy();
}
event.preventDefault();
event.stopPropagation();
});
if (!this.disableClose) {
this.element.querySelector(".b3-dialog__close").addEventListener("click", (event) => {
this.destroy();
event.preventDefault();
event.stopPropagation();
});
}
document.body.append(this.element);
if (options.disableAnimation) {
this.element.classList.add("b3-dialog--open");
} else {
setTimeout(() => {
this.element.classList.add("b3-dialog--open");
});
}
/// #if !MOBILE
moveResize(this.element.querySelector(".b3-dialog__container"), options.resizeCallback);
/// #endif
}

public destroy(options?: IObject) {
unmountReactRootsArray(window.Sillot.android?.NewDailyNoteSelectReactRoots);
// av 修改列头emoji后点击关闭emoji图标
if ((this.element.querySelector(".b3-dialog") as HTMLElement).style.zIndex < window.siyuan.menus.menu.element.style.zIndex) {
// https://github.com/siyuan-note/siyuan/issues/6783
window.siyuan.menus.menu.remove();
}
this.element.remove();
if (this.destroyCallback) {
this.destroyCallback(options);
}
window.siyuan.dialogs.find((item, index) => {
if (item.id === this.id) {
window.siyuan.dialogs.splice(index, 1);
return true;
}
});
// https://github.com/siyuan-note/siyuan/issues/10475
document.getElementById("drag")?.classList.remove("fn__hidden");
}

public bindInput(inputElement: HTMLInputElement | HTMLTextAreaElement, enterEvent?: () => void, bindEnter = true) {
inputElement.focus();
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
if (event.isComposing) {
event.preventDefault();
return;
}
if (event.key === "Escape") {
this.destroy();
event.preventDefault();
event.stopPropagation();
return;
}
if (!event.shiftKey && isNotCtrl(event) && event.key === "Enter" && enterEvent && bindEnter) {
enterEvent();
event.preventDefault();
event.stopPropagation();
}
});
}
}
2 changes: 1 addition & 1 deletion app/src/mobile/menu/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export const initRightMenu = (app: App) => {
break;
} else if (target.id === "menuNewDaily") {
newDailyNote(app);
closePanel();
// closePanel();
event.preventDefault();
event.stopPropagation();
break;
Expand Down
2 changes: 1 addition & 1 deletion app/src/mobile/settings/appearance_react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const initAppearanceReact = () => {
const root = Client.createRoot(e);
root.render(<AppearanceSettingsProvider />);
if (!window.Sillot.android.AppearanceReactRoots) {
window.Sillot.android.AppearanceReactRoots = []; // 初始化roots数组
window.Sillot.android.AppearanceReactRoots = [];
}
window.Sillot.android.AppearanceReactRoots.push(root); // 在 window.goBakc() 、closePanel 和 closeModel 中 unmount
},
Expand Down
2 changes: 1 addition & 1 deletion app/src/mobile/settings/devOptions_react.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const initDevOptionsReact = () => {
const root = Client.createRoot(e);
root.render(<DevOptionsProvider />);
if (!window.Sillot.android.DevOptionsReactRoots) {
window.Sillot.android.DevOptionsReactRoots = []; // 初始化roots数组
window.Sillot.android.DevOptionsReactRoots = [];
}
window.Sillot.android.DevOptionsReactRoots.push(root); // 在 window.goBakc() 、closePanel 和 closeModel 中 unmount
},
Expand Down
18 changes: 8 additions & 10 deletions app/src/util/mount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {setStorageVal} from "../protyle/util/compatibility";
import {openFileById} from "../editor/util";
import {openMobileFileById} from "../mobile/editor";
import type {App} from "../index";
import { renderNewDailyNoteSelect } from "./mount_react";
import { closePanel } from "../mobile/util/closePanel";

export const fetchNewDailyNote = (app: App, notebook: string) => {
window.sout.tracker("-> notebook: ", notebook);
Expand All @@ -24,7 +26,7 @@ export const fetchNewDailyNote = (app: App, notebook: string) => {
});
};

export const newDailyNote = (app: App) => {
export const newDailyNote = async (app: App) => {
const exit = window.siyuan.dialogs.find(item => {
if (item.element.getAttribute("data-key") === Constants.DIALOG_DIALYNOTE) {
item.destroy();
Expand Down Expand Up @@ -58,37 +60,33 @@ export const newDailyNote = (app: App) => {
if (localNotebookId && localNotebookIsOpen && !isMobile()) {
fetchNewDailyNote(app, localNotebookId);
} else {
let optionsHTML = "";
window.siyuan.notebooks.forEach(item => {
if (!item.closed) {
optionsHTML += `<option value="${item.id}">${item.name}</option>`;
}
});
const dialog = new Dialog({
positionId: Constants.DIALOG_DIALYNOTE,
title: window.siyuan.languages.plsChoose,
content: `<div class="b3-dialog__content">
<select class="b3-select fn__block">${optionsHTML}</select>
<div id="renderNewDailyNoteSelect_container"/>
</div>
<div class="b3-dialog__action">
<button class="b3-button b3-button--cancel">${window.siyuan.languages.cancel}</button><div class="fn__space"></div>
<button class="b3-button b3-button--text">${window.siyuan.languages.confirm}</button>
</div>`,
width: isMobile() ? "92vw" : "520px",
});
const renderNewDailyNoteSelect_container = dialog.element.querySelector("#renderNewDailyNoteSelect_container") as HTMLElement;
await renderNewDailyNoteSelect(window.siyuan.notebooks, localNotebookId, renderNewDailyNoteSelect_container);
dialog.element.setAttribute("data-key", Constants.DIALOG_DIALYNOTE);
const btnsElement = dialog.element.querySelectorAll(".b3-button");
const selectElement = dialog.element.querySelector(".b3-select") as HTMLSelectElement;
selectElement.value = localNotebookId;
btnsElement[0].addEventListener("click", () => {
dialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
const selectElement = dialog.element.querySelector(".selectedNotebook > input") as HTMLSelectElement;
const notebook = selectElement.value;
window.siyuan.storage[Constants.LOCAL_DAILYNOTEID] = notebook;
setStorageVal(Constants.LOCAL_DAILYNOTEID, window.siyuan.storage[Constants.LOCAL_DAILYNOTEID]);
fetchNewDailyNote(app, notebook);
dialog.destroy();
closePanel();
});
}
};
Expand Down
41 changes: 41 additions & 0 deletions app/src/util/mount_react.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import * as React from "react";
import * as Client from "react-dom/client";
import { Option } from "@mui/joy";
import { Select_KeyboardArrowDown } from "../sillot/joyUI/base/selector";

export const renderNewDailyNoteSelect = async (notebooks, defaultValue, container: HTMLElement) => {
console.log("defaultValue", defaultValue);
const root = Client.createRoot(container);
root.render(<Select_newDailyNote notebooks={notebooks} defaultValue={defaultValue} />);
if (!window.Sillot.android.NewDailyNoteSelectReactRoots) {
window.Sillot.android.NewDailyNoteSelectReactRoots = [];
}
window.Sillot.android.NewDailyNoteSelectReactRoots.push(root); // 在 dialog.destroy() 中 unmount
};

const Select_newDailyNote = ({ notebooks, defaultValue }) => {
const [selectedNotebookId, setSelectedNotebookId] = React.useState("");

const handleNotebookChange = (e, v) => {
setSelectedNotebookId(v);
};

return (
<Select_KeyboardArrowDown
className="selectedNotebook fn__block"
defaultValue={defaultValue}
value={selectedNotebookId}
onChange={handleNotebookChange}
>
{notebooks.map(item => {
if (!item.closed) {
return (
<Option key={item.id} value={item.id}>
{item.name}
</Option>
);
}
})}
</Select_KeyboardArrowDown>
);
};

0 comments on commit ac507de

Please sign in to comment.