-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
143 lines (143 loc) · 4.99 KB
/
main.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
const ob = require('obsidian')
, { SearchCursor, RegExpCursor } = require('@codemirror/search')
, { Decoration, ViewPlugin } = require('@codemirror/view')
, S = String.raw
, DBCMarks = '[!,。?;:()《》【】「」、]'
, editing = {
'SP2': ' {2}',
'SP4': ' {4}',
'DBC': S`${DBCMarks} (?!\^|#)|(?<! |#|^ *?(-|\d+?\.)) ${DBCMarks}`,
'eng': '[]',
}
, reading = {
'cm-query': '[]',
}
, all = Object.assign(editing, reading)
module.exports = class extends ob.Plugin {
onload() {
import_onSelect().call(this)
import_onQuery(this.app, all).call(this)
inPreview.call(this, reading)
}
onunload() {}
}
const import_onSelect = ()=> {
const mark = (view, query, cls, flag, maxMatches)=> {
const r = []
for (const port of view.visibleRanges) {
const matcher = new SearchCursor(view.state.doc, query, port.from, port.to, s=> s.toLowerCase())
while (!matcher.next().done) {
const { from, to } = matcher.value
if (flag(from, to)) r.push(Decoration.mark({class: cls}).range(from, to))
if (r.length > maxMatches) return []
}
}
return r
}
class onSelect {
minSelectLen = 1; maxSelectLen = 200
update(update) {
if (update.docChanged || update.selectionSet || update.viewportChanged) {
update.docChanged ? this.decos = null : setTimeout(()=> this.decos = null, 100)
this.getDecos2(update.view)
}
}
getDecos2 = (ob.debounce)(this.getDecos, 150, !0)
getDecos(view) {
const { ranges, main: range } = view.state.selection
if (range.empty) return; const len = range.to - range.from, decos = []
if (ranges.length < 2 && this.minSelectLen <= len && len <= this.maxSelectLen) {
const slc = view.state.sliceDoc(range.from, range.to).trim()
decos.push(...mark(view, slc, 'cm-select', (from, to)=> from >= range.to || to <= range.from, 200))
}
decos.push(...mark(view, ' ', 'SP1', (from, to)=> from < range.to && to > range.from))
this.decos = Decoration.set(decos, !0); view.update([])
}
}
return function() {
this.registerEditorExtension([
ViewPlugin.fromClass(onSelect, {decorations: plug=> plug.decos || Decoration.none})
])
}
}
const eq = (obj1, obj2)=> JSON.stringify(obj1) === JSON.stringify(obj2)
const clone = (obj)=> {
const cloned = {}; for (const key in obj) cloned[key] = obj[key]; return cloned
}
const import_onQuery = (app, opts)=> {
const updateConf = ()=> {
app.workspace.iterateAllLeaves(leaf=> {
const view = leaf.view?.currentMode?.cm
if (view) view.dispatch()
})
}
const 结果高亮 = (editor)=> {
const str = editor.getSelection()
if (opts.T1 == '[]' && !str) return
opts.T1 = str || '[]'; updateConf()
}
const 中英切换 = ()=> {
opts.eng = opts.eng == '[]' ? '[a-zA-Z]+' : '[]'
updateConf()
}
const onQuery = class {
update(update) {
if (update.viewportChanged || update.docChanged || !eq(this.prev, opts)) {
this.decos = null; this.getDecos(update.view)
}
}
getDecos(view) {
const { state } = view, decos = []
for (const port of view.visibleRanges) {
for (const [cls, query] of Object.entries(opts)) {
const matcher = new RegExpCursor(state.doc, query, {}, port.from, port.to)
while (!matcher.next().done) {
const { from, to } = matcher.value
decos.push(Decoration.mark({class: cls}).range(from, to))
}
}
}
this.decos = Decoration.set(decos, !0); this.prev = clone(opts)
}
}
return function() {
[ ['deco-result', '结果高亮', 结果高亮],
['deco-eng', '中英切换', 中英切换],
].map(([id, name, editorCallback, hotkeys])=> this.addCommand({id, name, editorCallback, hotkeys}))
this.registerEditorExtension([
ViewPlugin.fromClass(onQuery, {decorations: plug=> plug.decos || Decoration.none})
])
}
}
function inPreview(opts) {
this.registerMarkdownPostProcessor(el=> {
const walker = document.createTreeWalker(el, NodeFilter.SHOW_TEXT)
let node; const matches = []
while (node = walker.nextNode()) {
const text = node.textContent?.trim()
if (!text) continue
for (const [cls, query] of Object.entries(opts)) {
if (new RegExp(query).test(text)) {
matches.push({node, cls, query}); break
}
}
}
matches.forEach(item=> spanTagText(item))
})
}
const spanTagText = ({node, cls, query})=> {
const parent = node.parentNode; if (!parent) return
const text = node.textContent, rgx = new RegExp(query, 'g')
let match, lastEnd = 0
while (match = rgx.exec(text)) {
const pre = text.slice(lastEnd, match.index)
if (pre) parent.insertBefore(document.createTextNode(pre), node)
lastEnd = rgx.lastIndex
const span = document.createElement('span')
Object.assign(span, {className: cls, textContent: match[0]})
parent.insertBefore(span, node)
}
const suf = text.slice(lastEnd)
if (suf) parent.insertBefore(document.createTextNode(suf), node)
node.remove()
}