From 528759f6f0b47bd0bebdb7c49bfbec63dbd56264 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Tue, 27 Jun 2023 00:46:08 +0800 Subject: [PATCH] website: add mark decoration example. --- www/src/pages/examples/MarkDecoration.tsx | 84 +++++++++++++++++++++++ www/src/router.tsx | 5 ++ 2 files changed, 89 insertions(+) create mode 100644 www/src/pages/examples/MarkDecoration.tsx diff --git a/www/src/pages/examples/MarkDecoration.tsx b/www/src/pages/examples/MarkDecoration.tsx new file mode 100644 index 000000000..73c90c438 --- /dev/null +++ b/www/src/pages/examples/MarkDecoration.tsx @@ -0,0 +1,84 @@ +import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror'; +import { useRef } from 'react'; + +import { Decoration, drawSelection, EditorView, lineNumbers, ViewPlugin, WidgetType } from '@codemirror/view'; + +class SimpleWidget extends WidgetType { + toDOM() { + const element = document.createElement('span'); + element.className = 'filler'; + return element; + } +} + +const extensions = [ + lineNumbers(), + drawSelection(), + ViewPlugin.define( + () => { + return { + decorations: Decoration.set([ + Decoration.widget({ + widget: new SimpleWidget(), + side: 1, // is after the cursor + }).range(34), + + Decoration.mark({ + class: 'wrapper', + }).range(60, 70), + + Decoration.widget({ + widget: new SimpleWidget(), + side: 1, // should be after the cursor + }).range(67), + ]), + }; + }, + { + decorations: (value) => value.decorations, + }, + ), + EditorView.baseTheme({ + '.filler': { + display: 'inline-block', + width: '8px', + height: '1em', + backgroundColor: '#9c9', + }, + '.wrapper': { + border: '1px solid #aaa', + }, + }), +]; + +const code = `The cursor is before this widget: + +The cursor is after this widget +dsddfsdfsdfdf +Is this how it should work?`; + +export function Component() { + let $edit = useRef(null); + function refCallack(editor: ReactCodeMirrorRef) { + if (!$edit.current && editor?.editor && editor?.state && editor?.view) { + // first time we got ref, similar to useEffect + console.log(editor); // do something with editor + $edit.current = editor; // store it + } + } + return ( +
+ +
+ ); +} + +Component.displayName = 'HomePage'; diff --git a/www/src/router.tsx b/www/src/router.tsx index 9627e9244..6658400c9 100644 --- a/www/src/router.tsx +++ b/www/src/router.tsx @@ -547,6 +547,11 @@ export const routes: MenuRouteObject[] = [ label: 'Refs Example', lazy: () => import('./pages/examples/ExampleRef'), }, + { + path: 'mark-decoration', + label: 'Mark Decoration Example', + lazy: () => import('./pages/examples/MarkDecoration'), + }, ], }, ],