|
| 1 | +import { EditorView, basicSetup } from "codemirror"; |
| 2 | +import { EditorState } from "@codemirror/state"; |
| 3 | +import { keymap } from "@codemirror/view"; |
| 4 | +import { codeEditorTheme } from "@/components/codemirror/theme"; |
| 5 | +import behaviorShim from "@/util/behavior-shim"; |
| 6 | + |
| 7 | +// Java language support — also used for Groovy (syntactically very similar, no CM6 Groovy parser exists) |
| 8 | +import { java } from "@codemirror/lang-java"; |
| 9 | + |
| 10 | +// Map CM2 mode names/MIME types to CM6 language support. |
| 11 | +// Only Java/Groovy is bundled in core. Plugins needing other languages |
| 12 | +// can contribute additional language support. |
| 13 | +const LANGUAGES = { |
| 14 | + groovy: java, |
| 15 | + "text/x-groovy": java, |
| 16 | + java: java, |
| 17 | + "text/x-java": java, |
| 18 | + clike: java, |
| 19 | + "text/x-csrc": java, |
| 20 | + "text/x-c++src": java, |
| 21 | + "text/x-csharp": java, |
| 22 | +}; |
| 23 | + |
| 24 | +function getLanguageSupport(mode) { |
| 25 | + if (!mode) { |
| 26 | + return null; |
| 27 | + } |
| 28 | + const factory = LANGUAGES[mode] ?? LANGUAGES[mode.toLowerCase()]; |
| 29 | + if (typeof factory === "function") { |
| 30 | + return factory(); |
| 31 | + } |
| 32 | + // Unknown modes get plain text editing (no syntax highlighting) |
| 33 | + return null; |
| 34 | +} |
| 35 | + |
| 36 | +function createEditor(textarea, options) { |
| 37 | + const mode = options.mode; |
| 38 | + const lineNumbers = |
| 39 | + options.lineNumbers !== undefined ? options.lineNumbers : false; |
| 40 | + const readOnly = textarea.hasAttribute("readonly"); |
| 41 | + |
| 42 | + const extensions = [basicSetup, codeEditorTheme]; |
| 43 | + |
| 44 | + const languageSupport = getLanguageSupport(mode); |
| 45 | + if (languageSupport) { |
| 46 | + extensions.push(languageSupport); |
| 47 | + } |
| 48 | + |
| 49 | + if (readOnly) { |
| 50 | + extensions.push(EditorState.readOnly.of(true)); |
| 51 | + } |
| 52 | + |
| 53 | + // Sync changes back to the textarea on every update |
| 54 | + extensions.push( |
| 55 | + EditorView.updateListener.of((update) => { |
| 56 | + if (update.docChanged) { |
| 57 | + textarea.value = update.state.doc.toString(); |
| 58 | + textarea.dispatchEvent(new Event("change")); |
| 59 | + } |
| 60 | + }), |
| 61 | + ); |
| 62 | + |
| 63 | + if (!lineNumbers) { |
| 64 | + extensions.push(EditorView.lineWrapping); |
| 65 | + } |
| 66 | + |
| 67 | + const view = new EditorView({ |
| 68 | + state: EditorState.create({ |
| 69 | + doc: textarea.value, |
| 70 | + extensions: extensions, |
| 71 | + }), |
| 72 | + }); |
| 73 | + |
| 74 | + // Handle Cmd/Ctrl+Enter to submit the form |
| 75 | + if (textarea.form) { |
| 76 | + const submitKeymap = keymap.of([ |
| 77 | + { |
| 78 | + key: "Mod-Enter", |
| 79 | + run: () => { |
| 80 | + textarea.value = view.state.doc.toString(); |
| 81 | + textarea.form.submit(); |
| 82 | + return true; |
| 83 | + }, |
| 84 | + }, |
| 85 | + ]); |
| 86 | + view.dispatch({ |
| 87 | + effects: EditorState.appendConfig.of(submitKeymap), |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + textarea.parentNode.insertBefore(view.dom, textarea); |
| 92 | + textarea.style.display = "none"; |
| 93 | + |
| 94 | + // Store reference on textarea for backward compatibility |
| 95 | + textarea.codemirrorObject = { |
| 96 | + getValue: () => view.state.doc.toString(), |
| 97 | + setValue: (text) => { |
| 98 | + view.dispatch({ |
| 99 | + changes: { from: 0, to: view.state.doc.length, insert: text }, |
| 100 | + }); |
| 101 | + }, |
| 102 | + setLine: (line, text) => { |
| 103 | + const lineObj = view.state.doc.line(line + 1); |
| 104 | + view.dispatch({ |
| 105 | + changes: { from: lineObj.from, to: lineObj.to, insert: text }, |
| 106 | + }); |
| 107 | + }, |
| 108 | + save: () => { |
| 109 | + textarea.value = view.state.doc.toString(); |
| 110 | + }, |
| 111 | + getView: () => view, |
| 112 | + }; |
| 113 | + |
| 114 | + // Sync value before form submission |
| 115 | + if (textarea.form) { |
| 116 | + textarea.form.addEventListener("submit", () => { |
| 117 | + textarea.value = view.state.doc.toString(); |
| 118 | + }); |
| 119 | + textarea.form.addEventListener("jenkins:apply", () => { |
| 120 | + textarea.value = view.state.doc.toString(); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + return view; |
| 125 | +} |
| 126 | + |
| 127 | +function init() { |
| 128 | + // Handle textareas with codemirror-mode attribute (from f:textarea) |
| 129 | + behaviorShim.specify( |
| 130 | + "TEXTAREA.codemirror", |
| 131 | + "codemirror-textarea", |
| 132 | + 0, |
| 133 | + (textarea) => { |
| 134 | + const mode = textarea.getAttribute("codemirror-mode") || "text/x-groovy"; |
| 135 | + let config = {}; |
| 136 | + const configAttr = textarea.getAttribute("codemirror-config"); |
| 137 | + if (configAttr) { |
| 138 | + try { |
| 139 | + config = JSON.parse("{" + configAttr + "}"); |
| 140 | + } catch { |
| 141 | + const match = configAttr.match("^mode: ?'([^']+)'$"); |
| 142 | + if (match) { |
| 143 | + config = { mode: match[1] }; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + createEditor(textarea, { |
| 148 | + mode: config.mode || mode, |
| 149 | + lineNumbers: false, |
| 150 | + }); |
| 151 | + }, |
| 152 | + ); |
| 153 | + |
| 154 | + // Handle Script Console textareas |
| 155 | + behaviorShim.specify( |
| 156 | + "TEXTAREA.script", |
| 157 | + "codemirror-script-console", |
| 158 | + 0, |
| 159 | + (textarea) => { |
| 160 | + const mode = textarea.getAttribute("script-mode") || "text/x-groovy"; |
| 161 | + createEditor(textarea, { |
| 162 | + mode: mode, |
| 163 | + lineNumbers: true, |
| 164 | + }); |
| 165 | + }, |
| 166 | + ); |
| 167 | +} |
| 168 | + |
| 169 | +export default { init }; |
0 commit comments