Skip to content

Commit

Permalink
fix the selection before replace #48
Browse files Browse the repository at this point in the history
  • Loading branch information
Benature committed Mar 16, 2024
1 parent 9e13c1b commit a11db1a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
49 changes: 31 additions & 18 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -599,8 +599,7 @@ export default class TextFormat extends Plugin {
editor.setSelections([{ anchor: editor.offsetToPos(aos), head: editor.offsetToPos(hos) }])
}

const selectedTextList: string[] = [];
const rangeList: EditorRangeOrCaret[] = []

let selectedText = editor.getSelection();

let from = editor.getCursor("from"),
Expand All @@ -609,6 +608,8 @@ export default class TextFormat extends Plugin {

let adjustSelection = "none";

let supportMultiCursor = false;

//: Adjust Selection
switch (cmd) {
case "capitalize-word":
Expand All @@ -622,6 +623,7 @@ export default class TextFormat extends Plugin {
if (origin_cursor_from.line != origin_cursor_to.line) {
adjustSelection = "whole-paragraph";
}
// supportMultiCursor = true;
break;
case "split-blank":
case "bullet":
Expand All @@ -642,22 +644,9 @@ export default class TextFormat extends Plugin {
break;
default:
// Except special process of adjusting selection, get all selected text (for now)
selectionList.forEach((selection) => {
selectedTextList.push(editor.getRange(selection.anchor, selection.head))
rangeList.push(selection2range(editor, selection))
});
// selectedTextList = editor.getSelection();
supportMultiCursor = true;
break;
}
// For command that need adjusting selection, only contain first selection
// TODO: heading should support multi-selection
if (selectedTextList.length === 0) {
// selectedTextList = [selectedText];
// selectionList = [selection];
selectedTextList.push(selectedText);
rangeList.push(selection2range(editor, selection))
}


switch (adjustSelection) {
case "whole-paragraph":
Expand All @@ -677,11 +666,30 @@ export default class TextFormat extends Plugin {
}]);
}
selectedText = editor.getSelection();
console.log(selectedText)
break;
default:
break;
}

// For command that need adjusting selection, only contain first selection
const selectedTextList: string[] = [];
const rangeList: EditorRangeOrCaret[] = [];
if (supportMultiCursor) {
selectionList.forEach((selection) => {
selectedTextList.push(editor.getRange(selection.anchor, selection.head))
rangeList.push(selection2range(editor, selection))
});
} else {
selectedTextList.push(selectedText);
rangeList.push(selection2range(editor, selection))
}

console.log(selectedTextList)
console.log(editor.getSelection())



let replacedTextList: string[] = [];
const changeList: EditorChange[] = [];

Expand Down Expand Up @@ -764,16 +772,21 @@ export default class TextFormat extends Plugin {
}
}
replacedTextList.push(replacedText);
changeList.push({ text: replacedText, ...rangeList[i] });
if (supportMultiCursor) {
changeList.push({ text: replacedText, ...rangeList[i] });
} else {
changeList.push({ text: replacedText, ...selection2range(editor, editor.listSelections()[0]) })
}
// })
}



editor.transaction({
selections: rangeList,
// selections: rangeList,
changes: changeList
});
console.log(changeList)


let replacedText = replacedTextList[0];
Expand Down
2 changes: 1 addition & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -581,7 +581,7 @@ export function convertLatex(editor: Editor, selectedText: string): string {
RegExp(pre + patternChar2 + suf, "g"),
(t, t1, t2) => {
// ignore cases
if (/is|or|as|to|am|an|at|by|do|go|ha|he|hi|ho|if|in|it|my|no|of|on|so|up|us|we/g.test(t)) { return t; }
if (/is|or|as|to|am|an|at|by|do|go|ha|he|hi|ho|if|in|it|my|no|of|on|so|up|us|we|be/g.test(t)) { return t; }
return `$${G(t1)}_${G(t2)}$`;
})
.replace(
Expand Down

0 comments on commit a11db1a

Please sign in to comment.