-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using codemirror for copy template text-area highlighting (#28)
* codemirror basic setup * feat: wire-up save * chore: lint fix
- Loading branch information
Showing
7 changed files
with
309 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
...av/CustomizeActionsModal/EditActionSection/TemplateStringCreator/TemplateEditor/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { useEffect, useRef } from 'react'; | ||
import { EditorState } from '@codemirror/state'; | ||
import { javascript } from '@codemirror/lang-javascript'; | ||
import { oneDark } from '@codemirror/theme-one-dark'; | ||
import type { Extension } from '@codemirror/state'; | ||
import type { ViewUpdate } from '@codemirror/view'; | ||
import { keymap, drawSelection, EditorView } from '@codemirror/view'; | ||
import { history, historyKeymap } from '@codemirror/history'; | ||
import { indentOnInput } from '@codemirror/language'; | ||
import { bracketMatching } from '@codemirror/matchbrackets'; | ||
import { closeBrackets, closeBracketsKeymap } from '@codemirror/closebrackets'; | ||
import { autocompletion, completionKeymap } from '@codemirror/autocomplete'; | ||
import { defaultKeymap } from '@codemirror/commands'; | ||
import { commentKeymap } from '@codemirror/comment'; | ||
import './styles.css'; | ||
|
||
interface EditorProps { | ||
value?: string; | ||
onUpdate?: (update: ViewUpdate) => void; | ||
} | ||
|
||
export const TemplateEditor = ({ | ||
value = '', | ||
onUpdate = undefined, | ||
}: EditorProps) => { | ||
const editor = useRef(null); | ||
|
||
useEffect(() => { | ||
const currentEditor = editor.current as Exclude< | ||
typeof editor['current'], | ||
null | ||
>; | ||
const extensions: Extension[] = [ | ||
history(), | ||
drawSelection(), | ||
indentOnInput(), | ||
bracketMatching(), | ||
closeBrackets(), | ||
autocompletion(), | ||
oneDark, | ||
javascript(), | ||
keymap.of([ | ||
...closeBracketsKeymap, | ||
...defaultKeymap, | ||
...historyKeymap, | ||
...commentKeymap, | ||
...completionKeymap, | ||
]), | ||
]; | ||
if (onUpdate) extensions.push(EditorView.updateListener.of(onUpdate)); | ||
|
||
const state = EditorState.create({ | ||
doc: value, | ||
extensions, | ||
}); | ||
const view = new EditorView({ | ||
state, | ||
parent: currentEditor, | ||
}); | ||
|
||
return () => view.destroy(); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [editor]); | ||
|
||
return <div ref={editor} />; | ||
}; |
23 changes: 23 additions & 0 deletions
23
...v/CustomizeActionsModal/EditActionSection/TemplateStringCreator/TemplateEditor/styles.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
.cm-editor { | ||
@apply h-20 p-1 text-xl; | ||
} | ||
|
||
.cm-editor.cm-focused { | ||
outline: none; | ||
} | ||
.cm-editor > .cm-scroller:focus { | ||
outline: none; | ||
} | ||
|
||
.cm-scroller::-webkit-scrollbar { | ||
height: 10px; | ||
} | ||
|
||
.cm-scroller::-webkit-scrollbar-track { | ||
@apply bg-black3 rounded-full; | ||
} | ||
|
||
.cm-scroller::-webkit-scrollbar-thumb { | ||
@apply rounded-full; | ||
background-image: linear-gradient(180deg, #696eff 0%, #f7abff 100%); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters