Skip to content

Commit

Permalink
🎨 #13244
Browse files Browse the repository at this point in the history
  • Loading branch information
Vanessa219 authored and 88250 committed Nov 25, 2024
1 parent 9dade3b commit 46b0867
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
37 changes: 17 additions & 20 deletions app/src/protyle/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@ import {
hasClosestByAttribute,
hasClosestByClassName,
hasClosestByMatchTag,
hasTopClosestByClassName, isInEmbedBlock,
hasTopClosestByClassName,
isInEmbedBlock,
} from "../util/hasClosest";
import {
focusBlock,
focusByRange,
focusByWbr,
focusSideBlock,
getEditorRange,
getSelectionOffset, setFirstNodeRange,
getSelectionOffset,
setFirstNodeRange,
setLastNodeRange,
} from "../util/selection";
import {Constants} from "../../constants";
Expand All @@ -23,7 +25,8 @@ import {
contentMenu,
enterBack,
fileAnnotationRefMenu,
imgMenu, inlineMathMenu,
imgMenu,
inlineMathMenu,
linkMenu,
refMenu,
setFold,
Expand Down Expand Up @@ -2004,16 +2007,7 @@ export class WYSIWYG {

// 输入法测试点 https://github.com/siyuan-note/siyuan/issues/3027
let isComposition = false; // for iPhone
this.element.addEventListener("compositionstart", (event) => {
// 搜狗输入法划选输入后无 data https://github.com/siyuan-note/siyuan/issues/4672
const range = getEditorRange(protyle.wysiwyg.element);
const nodeElement = hasClosestBlock(range.startContainer);
if (nodeElement && typeof protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] === "undefined") {
range.insertNode(document.createElement("wbr"));
protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] = nodeElement.outerHTML;
nodeElement.querySelector("wbr").remove();
}
isComposition = true;
this.element.addEventListener("compositionstart", (event) => {isComposition = true;
event.stopPropagation();
});

Expand Down Expand Up @@ -2085,23 +2079,26 @@ export class WYSIWYG {
this.element.addEventListener("keyup", (event) => {
const range = getEditorRange(this.element).cloneRange();
const nodeElement = hasClosestBlock(range.startContainer);
if (event.key !== "PageUp" && event.key !== "PageDown" && event.key !== "Home" && event.key !== "End" && event.key.indexOf("Arrow") === -1 &&
event.key !== "Alt" && event.key !== "Shift" && event.key !== "CapsLock" && event.key !== "Escape" && event.key !== "Meta" && !/^F\d{1,2}$/.test(event.key) &&
(!event.isComposing || (event.isComposing && range.toString() !== "")) // https://github.com/siyuan-note/siyuan/issues/4341
) {

if ( event.key !== "PageUp" && event.key !== "PageDown" && event.key !== "Home" && event.key !== "End" &&
event.key.indexOf("Arrow") === -1 && event.key !== "Escape" && event.key !== "Shift" &&
event.key !== "Meta" && event.key !== "Alt" && event.key !== "Control" && event.key !== "CapsLock" &&
!event.ctrlKey && !event.shiftKey && !event.metaKey && !event.altKey&&
!/^F\d{1,2}$/.test(event.key) ){
// 搜狗输入法不走 keydown,需重新记录历史状态
if (range.toString() === "" && // windows 下回车新建块输入abc,选中 bc ctrl+m 后光标错误
(!this.preventKeyup || event.code === "Space") &&
nodeElement && typeof protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] === "undefined") {
if ( nodeElement &&
( typeof protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] === "undefined"||range.toString()!==""||!this.preventKeyup)) {
range.insertNode(document.createElement("wbr"));
protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] = nodeElement.outerHTML;
nodeElement.querySelector("wbr").remove();
}
this.preventKeyup = false;
return;
}

// 需放在 lastHTMLs 后,否则 https://github.com/siyuan-note/siyuan/issues/4388
if (this.preventKeyup) {
this.preventKeyup = false;
return;
}

Expand Down
1 change: 1 addition & 0 deletions app/src/protyle/wysiwyg/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@ const updateInput = (html: string, protyle: IProtyle, id: string) => {
data: protyle.wysiwyg.lastHTMLs[id],
action: "update"
});
protyle.wysiwyg.lastHTMLs[id] = item.outerHTML;
} else {
let firstElement;
if (index === 0) {
Expand Down
5 changes: 2 additions & 3 deletions app/src/protyle/wysiwyg/keydown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,14 @@ export const keydown = (protyle: IProtyle, editorElement: HTMLElement) => {
// 有可能输入 shift+. ,因此需要使用 event.key 来进行判断
if (event.key !== "PageUp" && event.key !== "PageDown" && event.key !== "Home" && event.key !== "End" && event.key.indexOf("Arrow") === -1 &&
event.key !== "Escape" && event.key !== "Shift" && event.key !== "Meta" && event.key !== "Alt" && event.key !== "Control" && event.key !== "CapsLock" &&
!isNotEditBlock(nodeElement) && !/^F\d{1,2}$/.test(event.key)) {
!isNotEditBlock(nodeElement) && !event.ctrlKey && !event.shiftKey && !event.metaKey && !event.altKey&&!/^F\d{1,2}$/.test(event.key)) {
const cloneRange = range.cloneRange();
range.insertNode(document.createElement("wbr"));
protyle.wysiwyg.lastHTMLs[nodeElement.getAttribute("data-node-id")] = nodeElement.outerHTML;
nodeElement.querySelector("wbr").remove();
// 光标位于引用结尾后 ctrl+b 偶尔会失效
range = cloneRange;
// 会导致 protyle.toolbar.range 和 range 不一致,先在有问题的地方重置一下 https://github.com/siyuan-note/siyuan/issues/10933
protyle.wysiwyg.preventKeyup = true; // 搜狗输入法进入此代码记录的话,keyup 就不再记录,否则 transaction 清空后 keyup 再次记录,下一次 keydown 就不会记录 https://github.com/siyuan-note/siyuan/issues/13244
protyle.wysiwyg.preventKeyup = true;
}

if (!window.siyuan.menus.menu.element.classList.contains("fn__none") &&
Expand Down
1 change: 0 additions & 1 deletion app/src/protyle/wysiwyg/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,6 @@ export const transaction = (protyle: IProtyle, doOperations: IOperation[], undoO
protyle.transactionTime - time < Constants.TIMEOUT_INPUT) {
needDebounce = true;
}
protyle.wysiwyg.lastHTMLs = {};
if (undoOperations) {
if (window.siyuan.config.fileTree.openFilesUseCurrentTab && protyle.model) {
protyle.model.headElement.classList.remove("item--unupdate");
Expand Down

0 comments on commit 46b0867

Please sign in to comment.