-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlt+2-6 Insert title.js
145 lines (131 loc) · 5.2 KB
/
Alt+2-6 Insert title.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const heads = ['heading2', 'heading3', 'heading4', 'heading5', 'heading6'];
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 && ['2','3','4','5','6'].includes(event.key)) {
event.stopImmediatePropagation(); // Prevent being monitored multiple times. This is a trilium bug. The reason has not yet been found.
const elName = this.getElName();
if (elName.startsWith('paragraph') || (elName.startsWith('head') && !elName.endsWith(event.key))){
this.changeCurrentBlockToParagraph();
this.changeCurrentBlockToHeading(`heading${event.key}`);
}
if (elName.startsWith('head') && elName.endsWith(event.key)){console.log(2)
this.changeCurrentBlockToParagraph();
}
}
if (event.altKey && ['1'].includes(event.key)) {
event.stopImmediatePropagation();
this.changeCurrentBlockToParagraph();
}
});
}
}
}
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());
});
}
getElName() { //Get what level the title is
const selection = this.textEditor.model.document.selection;
const position = selection.getFirstPosition();
const parentElement = position.parent;
return parentElement.name;
}
changeCurrentBlockToHeading(headingType = 'heading2') {
const model = this.textEditor.model;
model.change(writer => {
const selection = model.document.selection;
const position = selection.getFirstPosition();
const block = position.findAncestor('paragraph'); // find paragraph
if (block) {
writer.rename(block, headingType); // Rename paragraph tags to heading type
}
});
}
changeCurrentBlockToParagraph() {
const model = this.textEditor.model;
model.change(writer => {
const selection = model.document.selection;
const position = selection.getFirstPosition();
let block;
for (let head of heads) {
block = position.findAncestor(head);
if (block) {
writer.rename(block, 'paragraph'); // Rename title tags to paragraphs
break;
}
}
});
}
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 false;
return range.start.offset === 0 && range.end.offset === startParent.maxOffset;
}
inserMath(text) {
this.textEditor.model.change(writer => {
this.delSelection();
const modelFragment = this.textEditor.data.processor.toView(
`<h2>${text}</h2>`
);
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);
});
}
}