-
Notifications
You must be signed in to change notification settings - Fork 0
/
Alt+F Convert selected text.js
115 lines (105 loc) · 3.9 KB
/
Alt+F Convert selected text.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
module.exports = class extends api.NoteContextAwareWidget {
get position() {
return 50;
}
static get parentWidget() {
return 'note-detail-pane';
}
constructor() {
super();
}
isEnabled() {
return super.isEnabled()
&& this.note.type === 'text';
}
doRender() {
this.$widget = $('');
return this.$widget;
}
async refreshWithNote(note) {
if (this.note.type === 'text') {
let textEditor = await this.getEditor();
this.textEditor = textEditor;
if (textEditor !== null) {
const input = $(`.note-split[data-ntx-id="${this.noteContext.ntxId}"]`).find('.note-detail-editable-text-editor');
input.on('keydown', (event) => {
if (event.altKey && event.key === 'f') {
const selectedText = this.getSelectedText();
event.stopImmediatePropagation();
if (selectedText !== '') {
if(this.isFullParagraphSelected()){
this.inserMath(selectedText);}
else{
this.inserInlineMath(selectedText);}
}
}
});
}
}
}
getSelectedText() {
const selection = window.getSelection();
if (!selection.rangeCount) return '';
const range = selection.getRangeAt(0);
const selectedText = selection.toString();
return selectedText;
}
delSelection() {
const selection = this.textEditor.model.document.selection;
const writer = this.textEditor.model.change(writer => {
writer.remove(selection.getFirstRange());
});
}
isFullParagraphSelected() {
const selection = this.textEditor.model.document.selection;
const range = selection.getFirstRange();
if (!range) return false;
const startParent = range.start.parent;
const endParent = range.end.parent;
if (startParent !== endParent) return true; //The paragraph selected by triple-click spans two paragraphs
return range.start.offset === 0 && range.end.offset === startParent.maxOffset;
}
inserInlineMath(text) {
text = text.replace(/^\${2,}|\${2,}$/g, "");
text = text.replace(/^[$]+|[$]+$/g, "");
this.textEditor.model.change(writer => {
this.delSelection();
const modelFragment = this.textEditor.data.processor.toView(
`<span class="math-tex">\\(${text}\\)</span>`
);
const modelElement = this.textEditor.data.toModel(modelFragment);
this.textEditor.model.insertContent(modelElement);
});
}
inserMath(text) {console.log(111);
this.textEditor.model.change(writer => {
this.delSelection();
const modelFragment = this.textEditor.data.processor.toView(
`<span class="math-tex">\\[${text}\\]</span>`
);
const modelElement = this.textEditor.data.toModel(modelFragment);
this.textEditor.model.insertContent(modelElement);
});
}
async getEditor() {
return new Promise((resolve, reject) => {
const maxRetries = 5;
const delay = 200;
let attempts = 0;
const intervalId = setInterval(async () => {
if (attempts >= maxRetries) {
clearInterval(intervalId);
resolve(null);
return;
}
const editor = await this.noteContext.getTextEditor();
if (editor !== null) {
clearInterval(intervalId);
resolve(editor);
} else {
attempts++;
}
}, delay);
});
}
}