Skip to content

Commit 528759f

Browse files
committed
website: add mark decoration example.
1 parent 19629e6 commit 528759f

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror';
2+
import { useRef } from 'react';
3+
4+
import { Decoration, drawSelection, EditorView, lineNumbers, ViewPlugin, WidgetType } from '@codemirror/view';
5+
6+
class SimpleWidget extends WidgetType {
7+
toDOM() {
8+
const element = document.createElement('span');
9+
element.className = 'filler';
10+
return element;
11+
}
12+
}
13+
14+
const extensions = [
15+
lineNumbers(),
16+
drawSelection(),
17+
ViewPlugin.define(
18+
() => {
19+
return {
20+
decorations: Decoration.set([
21+
Decoration.widget({
22+
widget: new SimpleWidget(),
23+
side: 1, // is after the cursor
24+
}).range(34),
25+
26+
Decoration.mark({
27+
class: 'wrapper',
28+
}).range(60, 70),
29+
30+
Decoration.widget({
31+
widget: new SimpleWidget(),
32+
side: 1, // should be after the cursor
33+
}).range(67),
34+
]),
35+
};
36+
},
37+
{
38+
decorations: (value) => value.decorations,
39+
},
40+
),
41+
EditorView.baseTheme({
42+
'.filler': {
43+
display: 'inline-block',
44+
width: '8px',
45+
height: '1em',
46+
backgroundColor: '#9c9',
47+
},
48+
'.wrapper': {
49+
border: '1px solid #aaa',
50+
},
51+
}),
52+
];
53+
54+
const code = `The cursor is before this widget:
55+
56+
The cursor is after this widget
57+
dsddfsdfsdfdf
58+
Is this how it should work?`;
59+
60+
export function Component() {
61+
let $edit = useRef<ReactCodeMirrorRef | null>(null);
62+
function refCallack(editor: ReactCodeMirrorRef) {
63+
if (!$edit.current && editor?.editor && editor?.state && editor?.view) {
64+
// first time we got ref, similar to useEffect
65+
console.log(editor); // do something with editor
66+
$edit.current = editor; // store it
67+
}
68+
}
69+
return (
70+
<div>
71+
<CodeMirror
72+
value={code}
73+
theme="none"
74+
ref={refCallack}
75+
height="400px"
76+
width="100%"
77+
style={{ margin: '0 0 23px 0', flex: 1 }}
78+
extensions={[extensions]}
79+
/>
80+
</div>
81+
);
82+
}
83+
84+
Component.displayName = 'HomePage';

www/src/router.tsx

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,6 +547,11 @@ export const routes: MenuRouteObject[] = [
547547
label: 'Refs Example',
548548
lazy: () => import('./pages/examples/ExampleRef'),
549549
},
550+
{
551+
path: 'mark-decoration',
552+
label: 'Mark Decoration Example',
553+
lazy: () => import('./pages/examples/MarkDecoration'),
554+
},
550555
],
551556
},
552557
],

0 commit comments

Comments
 (0)