refactor(muya): centralize read-only select-all in selection (#5813) · marktext/marktext@2fabb3f · GitHub
Skip to content

Commit 2fabb3f

Browse files
authored
refactor(muya): centralize read-only select-all in selection (#5813)
1 parent a9adca0 commit 2fabb3f

4 files changed

Lines changed: 45 additions & 13 deletions

File tree

‎packages/muya/src/editor/__tests__/readOnly.spec.ts‎

Lines changed: 27 additions & 0 deletions

‎packages/muya/src/muya.ts‎

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -502,19 +502,6 @@ export class Muya {
502502
}
503503

504504
selectAll() {
505-
if (this.readOnly) {
506-
const first = this.editor.scrollPage?.firstContentInDescendant()?.domNode;
507-
const last = this.editor.scrollPage?.lastContentInDescendant()?.domNode;
508-
if (!first || !last)
509-
return;
510-
const range = document.createRange();
511-
range.setStart(first, 0);
512-
range.setEnd(last, last.childNodes.length);
513-
const selection = document.getSelection();
514-
selection?.removeAllRanges();
515-
selection?.addRange(range);
516-
return;
517-
}
518505
this.editor.selection.selectAll();
519506
}
520507

‎packages/muya/src/selection/TextSelection.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ class TextSelection {
168168
if (aBlock == null || fBlock == null)
169169
return;
170170

171+
if (this._muya.readOnly) {
172+
const first = aBlock.domNode;
173+
const last = fBlock.domNode;
174+
if (!first || !last)
175+
return;
176+
// Reading selections must preserve folding and avoid editor selection side effects.
177+
const range = this._doc.createRange();
178+
range.setStart(first, 0);
179+
range.setEnd(last, last.childNodes.length);
180+
this._selectRange(range);
181+
return;
182+
}
183+
171184
this.setSelection(
172185
{ offset: 0, block: aBlock, path: aBlock.path },
173186
{ offset: fBlock.text.length, block: fBlock, path: fBlock.path },

‎packages/muya/src/selection/index.ts‎

Lines changed: 5 additions & 0 deletions

0 commit comments

Comments
 (0)