-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathreact.tsx
More file actions
111 lines (98 loc) · 2.93 KB
/
Copy pathreact.tsx
File metadata and controls
111 lines (98 loc) · 2.93 KB
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
import { createPortal } from 'react-dom';
import { type ReactNode, useLayoutEffect } from 'react';
import { useHTMLElement, useLatest } from '@coze-editor/react-hooks';
import { useInjector } from '@coze-editor/react';
import { Decoration, EditorView, keymap, WidgetType } from '@codemirror/view';
import { clientRectsFor } from './dom';
class PrefixElementWidget extends WidgetType {
constructor(
readonly content:
| string
| HTMLElement
| ((view: EditorView) => HTMLElement),
) {
super();
}
toDOM(view: EditorView) {
const wrap = document.createElement('span');
wrap.className = 'cm-prefix-element';
// solved the cursor height problem(eg. cursor double height with two lines)
wrap.style.cssText = 'height: 0;';
wrap.appendChild(
typeof this.content === 'string'
? document.createTextNode(this.content)
: typeof this.content === 'function'
? this.content(view)
: this.content,
);
if (typeof this.content === 'string') {
wrap.setAttribute('aria-label', `prefix ${this.content}`);
} else {
wrap.setAttribute('aria-hidden', 'true');
}
return wrap;
}
coordsAt(dom: HTMLElement) {
const rects = dom.firstChild ? clientRectsFor(dom.firstChild) : [];
if (!rects.length) {
return null;
}
const style = window.getComputedStyle(dom.parentNode as HTMLElement);
const rect = rects[0];
const lineHeight = parseInt(style.lineHeight);
if (rect.bottom - rect.top > lineHeight * 1.5) {
return {
left: rect.left,
right: rect.right,
top: rect.top,
bottom: rect.top + lineHeight,
};
}
return rect;
}
ignoreEvent() {
return false;
}
}
interface PrefixElementProps {
deletable?: boolean;
onDelete?: () => void;
children?: ReactNode;
}
function PrefixElement({ deletable, onDelete, children }: PrefixElementProps) {
const element = useHTMLElement('span');
const latestElement = useLatest(element);
const latestDeletable = useLatest(deletable);
const latestOnDelete = useLatest(onDelete);
const injector = useInjector();
useLayoutEffect(() => {
function run(view: EditorView) {
if (
view.state.selection.main.empty &&
view.state.selection.main.head === 0 &&
latestDeletable.current === true &&
typeof latestOnDelete.current === 'function'
) {
latestOnDelete.current();
}
return false;
}
return injector.inject([
EditorView.decorations.of(
Decoration.set([
Decoration.widget({
side: -100,
block: false,
widget: new PrefixElementWidget(() => latestElement.current),
}).range(0),
]),
),
keymap.of([
{ key: 'Backspace', shift: run, run },
{ key: 'Meta-Backspace', shift: run, run },
]),
]);
}, []);
return createPortal(children, element);
}
export { PrefixElement };