Skip to content

Commit

Permalink
fix: 代码块内 Ctrl+A 全选文本时不应选中末尾的换行符
Browse files Browse the repository at this point in the history
fix #13994
  • Loading branch information
TCOTC committed Feb 3, 2025
1 parent 9175ca0 commit 1908eba
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
6 changes: 5 additions & 1 deletion app/src/protyle/util/selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
}
} else {
position = getSelectionOffset(editElement, nodeElement, range);
if (position.start !== 0 || position.end !== editElement.textContent.length) {
const isCode = editElement.parentElement.classList.contains("hljs");
if (position.start !== 0 || position.end !== (!isCode ? editElement.textContent.length : editElement.textContent.length - 1)) {
// 全选后 rang 不对 https://ld246.com/article/1654848722251
let firstChild = editElement.firstChild;
while (firstChild) {
Expand Down Expand Up @@ -98,6 +99,9 @@ export const selectAll = (protyle: IProtyle, nodeElement: Element, range: Range)
lastChild = lastChild.lastChild as HTMLElement;
}
}
if (isCode && lastChild.textContent?.charAt(lastChild.textContent.length - 1) === "\n") {
range.setEnd(range.endContainer, lastChild.textContent.length - 1);
}
// 列表回车后,左键全选无法选中
focusByRange(range);
protyle.toolbar.render(protyle, range);
Expand Down
4 changes: 1 addition & 3 deletions app/src/protyle/wysiwyg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,7 @@ export class WYSIWYG {
html = tempElement.innerHTML;
}
// 不能使用 commonAncestorContainer https://ld246.com/article/1643282894693
if (hasClosestByAttribute(range.startContainer, "data-type", "NodeCodeBlock")) {
textPlain = tempElement.textContent.replace(/\n$/, "");
} else if (hasClosestByMatchTag(range.startContainer, "CODE")) {
if (hasClosestByAttribute(range.startContainer, "data-type", "NodeCodeBlock") || hasClosestByMatchTag(range.startContainer, "CODE")) {
textPlain = tempElement.textContent;
} else {
textPlain = range.toString();
Expand Down

0 comments on commit 1908eba

Please sign in to comment.