2022-10-06 21:07:47 +08:00
|
|
|
import {listIndent, listOutdent} from "../../protyle/wysiwyg/list";
|
|
|
|
|
import {hasClosestBlock, hasClosestByMatchTag} from "../../protyle/util/hasClosest";
|
2022-10-06 22:27:35 +08:00
|
|
|
import {insertEmptyBlock} from "../../block/util";
|
|
|
|
|
import {moveToDown, moveToUp} from "../../protyle/wysiwyg/move";
|
|
|
|
|
import {Constants} from "../../constants";
|
|
|
|
|
import {focusByRange} from "../../protyle/util/selection";
|
2022-10-07 17:52:49 +08:00
|
|
|
import {scrollCenter} from "../../util/highlightById";
|
2022-10-06 21:07:47 +08:00
|
|
|
|
2022-10-06 17:14:04 +08:00
|
|
|
export const showKeyboardToolbar = (bottom = 0) => {
|
|
|
|
|
const toolbarElement = document.getElementById("keyboardToolbar");
|
|
|
|
|
toolbarElement.classList.remove("fn__none");
|
|
|
|
|
toolbarElement.style.bottom = bottom + "px";
|
2022-10-07 17:52:49 +08:00
|
|
|
let range: Range;
|
|
|
|
|
if (getSelection().rangeCount > 0) {
|
|
|
|
|
range = getSelection().getRangeAt(0);
|
|
|
|
|
}
|
|
|
|
|
if (!range || !window.siyuan.mobileEditor ||
|
|
|
|
|
!window.siyuan.mobileEditor.protyle.wysiwyg.element.contains(range.startContainer)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-07 18:09:52 +08:00
|
|
|
scrollCenter(window.siyuan.mobileEditor.protyle, undefined, false, (window.outerHeight - 65) / 2 - 30);
|
2022-10-06 21:08:32 +08:00
|
|
|
};
|
2022-10-06 17:30:43 +08:00
|
|
|
|
|
|
|
|
export const hideKeyboardToolbar = () => {
|
|
|
|
|
const toolbarElement = document.getElementById("keyboardToolbar");
|
|
|
|
|
toolbarElement.classList.add("fn__none");
|
2022-10-06 21:08:32 +08:00
|
|
|
};
|
2022-10-06 17:30:43 +08:00
|
|
|
|
|
|
|
|
export const initKeyboardToolbar = () => {
|
|
|
|
|
const toolbarElement = document.getElementById("keyboardToolbar");
|
2022-10-06 22:45:30 +08:00
|
|
|
toolbarElement.addEventListener("click", (event) => {
|
2022-10-06 21:08:32 +08:00
|
|
|
const target = event.target as HTMLElement;
|
|
|
|
|
const buttonElement = hasClosestByMatchTag(target, "BUTTON");
|
2022-10-06 21:07:47 +08:00
|
|
|
if (!buttonElement || !window.siyuan.mobileEditor) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-06 22:45:30 +08:00
|
|
|
if (window.siyuan.mobileEditor.protyle.disabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
event.stopPropagation();
|
2022-10-06 21:07:47 +08:00
|
|
|
const type = buttonElement.getAttribute("data-type");
|
2022-10-06 21:08:32 +08:00
|
|
|
const protyle = window.siyuan.mobileEditor.protyle;
|
2022-10-06 21:07:47 +08:00
|
|
|
if (type === "undo") {
|
2022-10-06 21:08:32 +08:00
|
|
|
protyle.undo.undo(protyle);
|
2022-10-06 21:07:47 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (type === "redo") {
|
2022-10-06 21:08:32 +08:00
|
|
|
protyle.undo.redo(protyle);
|
2022-10-06 21:07:47 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2022-10-06 21:08:32 +08:00
|
|
|
let range: Range;
|
2022-10-06 21:07:47 +08:00
|
|
|
if (getSelection().rangeCount > 0) {
|
2022-10-06 21:08:32 +08:00
|
|
|
range = getSelection().getRangeAt(0);
|
2022-10-06 21:07:47 +08:00
|
|
|
}
|
2022-10-06 22:27:35 +08:00
|
|
|
if (!range || (range && !protyle.wysiwyg.element.contains(range.startContainer))) {
|
2022-10-06 21:07:47 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const nodeElement = hasClosestBlock(range.startContainer);
|
|
|
|
|
if (!nodeElement) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-10-06 22:27:35 +08:00
|
|
|
|
|
|
|
|
if (type === "up") {
|
|
|
|
|
moveToUp(protyle, nodeElement, range);
|
|
|
|
|
focusByRange(range);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (type === "down") {
|
|
|
|
|
moveToDown(protyle, nodeElement, range)
|
|
|
|
|
focusByRange(range);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "before") {
|
|
|
|
|
insertEmptyBlock(protyle, "beforebegin");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (type === "after") {
|
|
|
|
|
insertEmptyBlock(protyle, "afterend");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (type === "clear") {
|
|
|
|
|
if (range.toString()) {
|
|
|
|
|
protyle.toolbar.setInlineMark(protyle, "clear", "toolbar");
|
2022-10-07 17:52:49 +08:00
|
|
|
} else if (range.startContainer.nodeType === 3 && range.startContainer.parentElement.tagName === "SPAN") {
|
2022-10-06 22:27:35 +08:00
|
|
|
range.setStartAfter(range.startContainer.parentElement);
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
range.insertNode(document.createTextNode(Constants.ZWSP));
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
}
|
2022-10-06 22:45:30 +08:00
|
|
|
focusByRange(range);
|
2022-10-06 22:27:35 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 21:07:47 +08:00
|
|
|
if (!nodeElement.parentElement.classList.contains("li") || nodeElement.getAttribute("data-type") === "NodeCodeBlock") {
|
2022-10-06 22:56:07 +08:00
|
|
|
focusByRange(range);
|
2022-10-06 21:07:47 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (type === "outdent") {
|
|
|
|
|
listOutdent(protyle, [nodeElement.parentElement], range);
|
2022-10-06 22:38:12 +08:00
|
|
|
focusByRange(range);
|
2022-10-06 21:07:47 +08:00
|
|
|
} else if (type === "indent") {
|
|
|
|
|
listIndent(protyle, [nodeElement.parentElement], range);
|
2022-10-06 22:38:12 +08:00
|
|
|
focusByRange(range);
|
2022-10-06 20:39:43 +08:00
|
|
|
}
|
2022-10-06 21:08:32 +08:00
|
|
|
});
|
|
|
|
|
};
|