-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
97 lines (79 loc) · 2.22 KB
/
index.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
import { ViewPlugin, EditorView } from '@codemirror/view'
let theme
theme = EditorView.baseTheme({ '.cm-ruler-w': { pointerEvents: 'none',
position: 'absolute',
top: 0,
left: 0,
height: '100%',
width: '100%',
overflow: 'hidden' },
'.cm-ruler-vert': { position: 'absolute',
top: 0,
height: '100%',
width: '1px',
borderLeft: '1px solid' } })
export
function make
(opts) {
let plugin, exts, ruler, w, el
class Plugin {
constructor
(view) {
construct(view)
}
update
(update) {
if (ruler && update.viewportChanged)
ruler.update(update.view)
}
destroy
() {
ruler.free()
}
}
function update
(view) {
let gutter, pad, line, col
col = opts.col ?? 100
gutter = view.contentDOM.getBoundingClientRect().x - view.dom.getBoundingClientRect().x
pad = 0
line = view.contentDOM.querySelector('.cm-line')
if (line)
pad = parseFloat(globalThis.window.getComputedStyle(line).getPropertyValue('padding-left'))
el.style.left = ((col * view.defaultCharacterWidth) + gutter + pad) + 'px'
}
function free
() {
w.remove()
ruler = null
}
function set
(view, name, val) {
if (name == 'col') {
opts.col = val
update(view)
}
}
function construct
(view) {
w = globalThis.document.createElement('div')
w.classList.add('cm-ruler-w')
el = globalThis.document.createElement('div')
el.classList.add('cm-ruler-vert')
w.appendChild(el)
view.dom.prepend(w)
update(view)
}
plugin = ViewPlugin.fromClass(Plugin)
exts = [ theme,
plugin ]
ruler = { get exts() {
return exts
},
//
free,
set,
update
}
return ruler
}