Skip to content

Commit

Permalink
fix: 🐛 Fix for CM6
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Dec 29, 2021
1 parent c8098a7 commit cb4b21f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
15 changes: 8 additions & 7 deletions main.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ declare module "obsidian" {
interface Editor {
cm: {
findWordAt: (pos: EditorPosition) => EditorSelection | null;
state: {
wordAt: (offset: number) => { fromOffset: number; toOffset: number };
viewState: {
state: {
wordAt: (offset: number) => { from: number; to: number };
};
};
getDoc: () => Doc;
getScrollInfo: () => { top: number; left: number; clientHeight: number };
Expand Down
11 changes: 6 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ export default class ACPlugin extends Plugin {

reconstructSels(sels: EditorSelectionOrCaret[]) {
return sels.map((sel) => {
return { anchor: sel.anchor, head: sel.head };
const { anchor, head } = sel;
return { anchor, head };
});
}

Expand All @@ -215,7 +216,7 @@ export default class ACPlugin extends Plugin {
const reconSelections = this.reconstructSels([...newSels]);
ed.setSelections(reconSelections);
}
this.clearOldSetNewMSpan(ed, ...newSels);
// this.clearOldSetNewMSpan(ed, ...newSels);
}

clearOldSetNewMSpan(ed: Editor, ...newSels: EditorSelectionOrCaret[]) {
Expand Down Expand Up @@ -272,11 +273,11 @@ export default class ACPlugin extends Plugin {
const wordRange = ed.cm.findWordAt(cursor);
[wordA, wordH] = [wordRange.anchor, wordRange.head];
toSelect = ed.getRange(wordA, wordH);
} else if (ed.cm?.state.wordAt) {
const { fromOffset, toOffset } = ed.cm.state.wordAt(
} else if (ed.cm?.viewState.state.wordAt) {
const { from, to } = ed.cm?.viewState.state.wordAt(
ed.posToOffset(cursor)
);
[wordA, wordH] = [ed.offsetToPos(fromOffset), ed.offsetToPos(toOffset)];
[wordA, wordH] = [ed.offsetToPos(from), ed.offsetToPos(to)];
toSelect = ed.getRange(wordA, wordH);
} else {
throw new Error("Cannot determine if cm5 or cm6");
Expand Down

0 comments on commit cb4b21f

Please sign in to comment.