Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix red spell check line is shown frequently when typing #470

Merged
merged 2 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/web/utils/cursorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,6 @@ function setCursorPosition(target: MarkdownTextInputElement, startIndex: number,
selection.setBaseAndExtent(range.startContainer, range.startOffset, range.endContainer, range.endOffset);
}

// Update the focused elements zIndex to make sure they are on top and the cursor is beeing set correctly
startTreeNode.element.style.zIndex = '1';
endTreeNode.element.style.zIndex = '1';

scrollIntoView(startTreeNode);
}

Expand Down
9 changes: 5 additions & 4 deletions src/web/utils/parserUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {HTMLMarkdownElement, MarkdownTextInputElement} from '../../MarkdownTextInput.web';
import {addNodeToTree} from './treeUtils';
import {addNodeToTree, updateTreeElementRefs} from './treeUtils';
import type {NodeType, TreeNode} from './treeUtils';
import type {PartialMarkdownStyle} from '../../styleUtils';
import {getCurrentCursorPosition, moveCursorToEnd, setCursorPosition} from './cursorUtils';
Expand Down Expand Up @@ -246,6 +246,7 @@ function moveCursor(isFocused: boolean, alwaysMoveCursorToTheEnd: boolean, curso
setCursorPosition(target, cursorPosition);
}
}

function updateInputStructure(
target: MarkdownTextInputElement,
text: string,
Expand Down Expand Up @@ -276,12 +277,12 @@ function updateInputStructure(
if (shouldForceDOMUpdate || targetElement.innerHTML !== dom.innerHTML) {
targetElement.innerHTML = '';
targetElement.innerText = '';
Array.from(dom.children).forEach((child) => {
targetElement.appendChild(child);
});
targetElement.innerHTML = dom.innerHTML;
}

updateTreeElementRefs(tree, targetElement);
targetElement.tree = tree;

moveCursor(isFocused, alwaysMoveCursorToTheEnd, cursorPosition, targetElement);
}

Expand Down
19 changes: 18 additions & 1 deletion src/web/utils/treeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,23 @@ function addNodeToTree(element: HTMLMarkdownElement, parentTreeNode: TreeNode, t
return item;
}

function updateTreeElementRefs(treeRoot: TreeNode, element: HTMLMarkdownElement) {
const stack: TreeNode[] = [treeRoot];
while (stack.length > 0) {
const node = stack.pop() as TreeNode;
stack.push(...node.childNodes);

const currentElement = element.querySelector(`[data-id="${node.orderIndex}"]`) as HTMLMarkdownElement;
node.element = currentElement;

node.childNodes.forEach((child) => {
stack.push(child);
});
}

return treeRoot;
}

function findHTMLElementInTree(treeRoot: TreeNode, element: HTMLElement): TreeNode | null {
if (element.hasAttribute?.('contenteditable')) {
return treeRoot;
Expand Down Expand Up @@ -98,6 +115,6 @@ function getTreeNodeByIndex(treeRoot: TreeNode, index: number): TreeNode | null
return null;
}

export {addNodeToTree, findHTMLElementInTree, getTreeNodeByIndex};
export {addNodeToTree, findHTMLElementInTree, getTreeNodeByIndex, updateTreeElementRefs};

export type {TreeNode, NodeType};
Loading