|
| 1 | +import React, { useRef, useEffect, useState } from 'react'; |
| 2 | + |
| 3 | +import { basicSetup } from 'codemirror'; |
| 4 | +import { EditorState, Compartment } from '@codemirror/state'; |
| 5 | +import { EditorView, keymap, lineNumbers, placeholder, Decoration, ViewPlugin, MatchDecorator } from '@codemirror/view'; |
| 6 | +import { indentWithTab, history } from '@codemirror/commands'; |
| 7 | +//import { json } from '@codemirror/lang-json'; |
| 8 | +import { defaultKeymap } from '@codemirror/commands'; |
| 9 | +import { syntaxHighlighting, defaultHighlightStyle, HighlightStyle, StreamLanguage } from '@codemirror/language'; |
| 10 | +import { autocompletion, CompletionContext } from '@codemirror/autocomplete'; |
| 11 | +import { tags, styleTags } from '@lezer/highlight'; |
| 12 | +import { isEqual } from 'lodash'; |
| 13 | + |
| 14 | +// Function to dynamically generate autocomplete options |
| 15 | +const createAutocompleteSource = (options) => { |
| 16 | + return (context) => { |
| 17 | + let word = context.matchBefore(/\{\{\w*$/); |
| 18 | + if (!word) return null; |
| 19 | + |
| 20 | + return { |
| 21 | + from: word.from, |
| 22 | + options: options.map((option) => ({ label: `{{${option}}}`, type: 'keyword' })), |
| 23 | + }; |
| 24 | + }; |
| 25 | +}; |
| 26 | + |
| 27 | +// Create a Compartment for autocomplete |
| 28 | +const autocompleteCompartment = new Compartment(); |
| 29 | + |
| 30 | +// Function to update autocomplete options |
| 31 | +const updateAutocompleteOptions = (view, newOptions) => { |
| 32 | + view.dispatch({ |
| 33 | + effects: autocompleteCompartment.reconfigure(autocompletion({ override: [createAutocompleteSource(newOptions)] })), |
| 34 | + }); |
| 35 | +}; |
| 36 | + |
| 37 | +// Custom styles to hide scrollbar |
| 38 | +const hideScrollbar = EditorView.theme({ |
| 39 | + '.cm-scroller': { |
| 40 | + overflowX: 'auto', |
| 41 | + overflowY: 'hidden', |
| 42 | + whiteSpace: 'nowrap', |
| 43 | + scrollbarWidth: 'none' /* For Firefox */, |
| 44 | + }, |
| 45 | + '.cm-content': { |
| 46 | + padding: '0', // Adjust padding to fit your needs |
| 47 | + overflow: 'auto', |
| 48 | + }, |
| 49 | + '.cm-scroller::-webkit-scrollbar': { |
| 50 | + display: 'none' /* For Chrome, Safari, and Opera */, |
| 51 | + }, |
| 52 | + '.cm-line': { |
| 53 | + padding: '0', // Adjust padding to fit your needs |
| 54 | + }, |
| 55 | + '&': { |
| 56 | + height: 'auto', // Adjust height to auto for single-line input |
| 57 | + }, |
| 58 | + '.cm-placeholder': { |
| 59 | + color: '#aaa', // Placeholder text color |
| 60 | + }, |
| 61 | +}); |
| 62 | + |
| 63 | +// Rebind the Enter key to do nothing |
| 64 | +const rebindEnterKey = keymap.of([ |
| 65 | + { |
| 66 | + key: 'Enter', |
| 67 | + run: () => true, // Return true to prevent the default action |
| 68 | + }, |
| 69 | +]); |
| 70 | + |
| 71 | +// Create a MatchDecorator to highlight {{text}} strings |
| 72 | +const decorator = new MatchDecorator({ |
| 73 | + // Regular expression to match {{text}} strings |
| 74 | + regexp: /{{[^}]*}}/g, |
| 75 | + decoration: Decoration.mark({ class: 'highlight' }), |
| 76 | +}); |
| 77 | + |
| 78 | +// View plugin to apply the MatchDecorator |
| 79 | +const highlightPlugin = ViewPlugin.fromClass( |
| 80 | + class { |
| 81 | + constructor(view) { |
| 82 | + this.decorations = decorator.createDeco(view); |
| 83 | + } |
| 84 | + update(update) { |
| 85 | + this.decorations = decorator.updateDeco(update, this.decorations); |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + decorations: (v) => v.decorations, |
| 90 | + }, |
| 91 | +); |
| 92 | + |
| 93 | +// Custom styles for highlighting |
| 94 | +const highlightStyle = EditorView.baseTheme({ |
| 95 | + '.highlight': { |
| 96 | + color: 'brown', |
| 97 | + }, |
| 98 | +}); |
| 99 | + |
| 100 | +export const TextEditor = ({ placeHolder, onChangeHandler, value, disableState, completionOptions, styles }) => { |
| 101 | + const editor1 = useRef(); |
| 102 | + const [view, setView] = useState(null); |
| 103 | + const [dynamicOptions, setDynamicOptions] = useState([]); |
| 104 | + |
| 105 | + if (view) { |
| 106 | + if (!isEqual(dynamicOptions, completionOptions)) { |
| 107 | + updateAutocompleteOptions(view, completionOptions); |
| 108 | + setDynamicOptions(completionOptions); |
| 109 | + } |
| 110 | + if (value != view.state.doc.toString()) { |
| 111 | + view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: value } }); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + const onUpdate = EditorView.updateListener.of((v) => { |
| 116 | + if (onChangeHandler) { |
| 117 | + onChangeHandler(v.state.doc.toString()); |
| 118 | + } |
| 119 | + }); |
| 120 | + |
| 121 | + useEffect(() => { |
| 122 | + const state = EditorState.create({ |
| 123 | + doc: value, |
| 124 | + extensions: [ |
| 125 | + //EditorView.lineWrapping, |
| 126 | + placeholder(placeHolder), |
| 127 | + //myAutocomplete, |
| 128 | + //basicSetup, |
| 129 | + keymap.of([defaultKeymap, indentWithTab]), |
| 130 | + onUpdate, |
| 131 | + //EditorState.readOnly.of(props.readOnly || false), |
| 132 | + history(), |
| 133 | + hideScrollbar, |
| 134 | + rebindEnterKey, |
| 135 | + highlightPlugin, |
| 136 | + highlightStyle, |
| 137 | + autocompleteCompartment.of(autocompletion({ override: [createAutocompleteSource(dynamicOptions)] })), |
| 138 | + ], |
| 139 | + }); |
| 140 | + |
| 141 | + const view = new EditorView({ state, parent: editor1.current }); |
| 142 | + setView(view); |
| 143 | + |
| 144 | + return () => { |
| 145 | + view.destroy(); |
| 146 | + setView(null); |
| 147 | + }; |
| 148 | + }, []); |
| 149 | + |
| 150 | + const mainStyles = |
| 151 | + 'nodrag nowheel block rounded border border-slate-700 bg-background-light p-2.5 text-sm outline-none'; |
| 152 | + const intentStyles = disableState ? 'cursor-not-allowed text-slate-400' : 'text-slate-900'; |
| 153 | + |
| 154 | + return <div ref={editor1} className={`${mainStyles} ${intentStyles} ${styles}`}></div>; |
| 155 | +}; |
0 commit comments