2022-10-06 21:07:47 +08:00
|
|
|
import {getEventName} from "../../protyle/util/compatibility";
|
|
|
|
|
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-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-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 21:07:47 +08:00
|
|
|
toolbarElement.addEventListener(getEventName(), (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;
|
|
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
|
} else if (range.startContainer.nodeType === 3 && range.startContainer.parentElement.tagName === "SPAN"){
|
|
|
|
|
range.setStartAfter(range.startContainer.parentElement);
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
range.insertNode(document.createTextNode(Constants.ZWSP));
|
|
|
|
|
range.collapse(false);
|
|
|
|
|
focusByRange(range);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-06 21:07:47 +08:00
|
|
|
if (!nodeElement.parentElement.classList.contains("li") || nodeElement.getAttribute("data-type") === "NodeCodeBlock") {
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
};
|