0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-03-26 21:18:12 +00:00
This commit is contained in:
Víctor Losada Hernández
2026-03-25 17:23:38 +01:00
parent 456c149f7b
commit 52f2f532a7

View File

@@ -1,9 +1,6 @@
import "./codeEditor.less"; import './codeEditor.less';
import React, { useEffect, useRef, forwardRef, useImperativeHandle } from "react"; import React, { useEffect, useRef, forwardRef, useImperativeHandle } from 'react';
import { EditorState } from "@codemirror/state";
import { defaultKeymap, history, historyField, undo, redo } from "@codemirror/commands";
import { foldGutter, foldKeymap } from "@codemirror/language";
import { import {
EditorView, EditorView,
keymap, keymap,
@@ -11,65 +8,54 @@ import {
highlightActiveLineGutter, highlightActiveLineGutter,
highlightActiveLine, highlightActiveLine,
scrollPastEnd, scrollPastEnd,
} from "@codemirror/view"; Decoration,
import { markdown, markdownLanguage } from "@codemirror/lang-markdown"; ViewPlugin,
import { languages } from "@codemirror/language-data"; } from '@codemirror/view';
import { css } from "@codemirror/lang-css"; import { EditorState, Compartment } from '@codemirror/state';
import { basicLightHighlightStyle } from "cm6-theme-basic-light"; import { foldGutter, foldKeymap, syntaxHighlighting, HighlightStyle } from '@codemirror/language';
import { defaultKeymap, history, historyField, undo, redo } from '@codemirror/commands';
import { languages } from '@codemirror/language-data';
import { css } from '@codemirror/lang-css';
import { markdown, markdownLanguage } from '@codemirror/lang-markdown';
// themes import { tags } from '@lezer/highlight';
import * as themes from "@uiw/codemirror-themes-all"; // ######################### THEMES #############################
const themeList = Object.entries(themes) import * as themes from '@uiw/codemirror-themes-all';
.filter(([name, value]) => Array.isArray(value) && !name.endsWith("Init") && !name.endsWith("Style"))
.map(([name, theme]) => ({
name,
theme,
}));
console.log(themeList);
import { Compartment } from "@codemirror/state";
const themeCompartment = new Compartment(); const themeCompartment = new Compartment();
// custom highlighting // ######################### CUSTOM HIGHLIGHTS #############################
import { HighlightStyle } from "@codemirror/language";
import { syntaxHighlighting } from "@codemirror/language";
import { tags } from "@lezer/highlight";
const highlightStyle = HighlightStyle.define([ const highlightStyle = HighlightStyle.define([
{ {
tag : tags.heading1, tag : tags.heading1,
color: "black", color : 'black',
fontSize: "1.75em", fontSize : '1.75em',
fontWeight: "700", fontWeight : '700',
class: "cm-header cm-header-1", class : 'cm-header cm-header-1',
}, },
{ {
tag : tags.processingInstruction, tag : tags.processingInstruction,
color: "blue", color : 'blue',
}, },
// … // …
]); ]);
/*custom tokens */ import { tokenizeCustomMarkdown, customTags } from './customMarkdownGrammar.js';
import { Decoration, ViewPlugin, WidgetType } from "@codemirror/view";
import { tokenizeCustomMarkdown, customTags } from "./customMarkdownGrammar.js";
const customHighlightStyle = HighlightStyle.define([ const customHighlightStyle = HighlightStyle.define([
{ tag: tags.heading1, color: "#000", fontWeight: "700" }, { tag: tags.heading1, color: '#000', fontWeight: '700' },
{ tag: tags.keyword, color: "#07a" }, // example for your markdown headings { tag: tags.keyword, color: '#07a' }, // example for your markdown headings
{ tag: customTags.pageLine, color: "#f0a" }, { tag: customTags.pageLine, color: '#f0a' },
{ tag: customTags.snippetBreak, class: "cm-snippet-break", color: "#0af" }, { tag: customTags.snippetBreak, class: 'cm-snippet-break', color: '#0af' },
{ tag: customTags.inlineBlock, class: "cm-inline-block", backgroundColor: "#fffae6" }, { tag: customTags.inlineBlock, class: 'cm-inline-block', backgroundColor: '#fffae6' },
{ tag: customTags.emoji, class: "cm-emoji", color: "#fa0" }, { tag: customTags.emoji, class: 'cm-emoji', color: '#fa0' },
{ tag: customTags.superscript, class: "cm-superscript", verticalAlign: "super", fontSize: "0.8em" }, { tag: customTags.superscript, class: 'cm-superscript', verticalAlign: 'super', fontSize: '0.8em' },
{ tag: customTags.subscript, class: "cm-subscript", verticalAlign: "sub", fontSize: "0.8em" }, { tag: customTags.subscript, class: 'cm-subscript', verticalAlign: 'sub', fontSize: '0.8em' },
{ tag: customTags.definitionTerm, class: "cm-dt", fontWeight: "bold", color: "#0a0" }, { tag: customTags.definitionTerm, class: 'cm-dt', fontWeight: 'bold', color: '#0a0' },
{ tag: customTags.definitionDesc, class: "cm-dd", color: "#070" }, { tag: customTags.definitionDesc, class: 'cm-dd', color: '#070' },
]); ]);
const customHighlightPlugin = ViewPlugin.fromClass( const customHighlightPlugin = ViewPlugin.fromClass(
@@ -111,14 +97,16 @@ const customHighlightPlugin = ViewPlugin.fromClass(
}, },
); );
// ######################### COMPONENT #############################
const CodeEditor = forwardRef( const CodeEditor = forwardRef(
( (
{ {
value = "", value = '',
onChange = ()=>{}, onChange = ()=>{},
language = "", language = '',
tab = "brewText", tab = 'brewText',
editorTheme = "default", editorTheme = 'default',
view, view,
style, style,
...props ...props
@@ -130,7 +118,6 @@ const CodeEditor = forwardRef(
const docsRef = useRef({}); const docsRef = useRef({});
const prevTabRef = useRef(tab); const prevTabRef = useRef(tab);
// --- init editor ---
const createExtensions = ({ onChange, language, editorTheme })=>{ const createExtensions = ({ onChange, language, editorTheme })=>{
const updateListener = EditorView.updateListener.of((update)=>{ const updateListener = EditorView.updateListener.of((update)=>{
if(update.docChanged) { if(update.docChanged) {
@@ -165,12 +152,12 @@ const CodeEditor = forwardRef(
}; };
const customKeymap = keymap.of([ const customKeymap = keymap.of([
{ key: "Mod-b", run: boldCommand }, { key: 'Mod-b', run: boldCommand },
{ key: "Mod-i", run: italicCommand }, { key: 'Mod-i', run: italicCommand },
]); ]);
const languageExtension = const languageExtension =
language === "css" ? css() : markdown({ base: markdownLanguage, codeLanguages: languages }); language === 'css' ? css() : markdown({ base: markdownLanguage, codeLanguages: languages });
const themeExtension = Array.isArray(themes[editorTheme]) ? themes[editorTheme] : []; const themeExtension = Array.isArray(themes[editorTheme]) ? themes[editorTheme] : [];
@@ -253,7 +240,8 @@ const CodeEditor = forwardRef(
} }
}, [value]); }, [value]);
useEffect(() => { //rebuild theme extension on theme change useEffect(()=>{
//rebuild theme extension on theme change
const view = viewRef.current; const view = viewRef.current;
if(!view) return; if(!view) return;
@@ -264,7 +252,6 @@ const CodeEditor = forwardRef(
}); });
}, [editorTheme]); }, [editorTheme]);
useImperativeHandle(ref, ()=>({ useImperativeHandle(ref, ()=>({
getValue : ()=>viewRef.current.state.doc.toString(), getValue : ()=>viewRef.current.state.doc.toString(),
@@ -310,7 +297,7 @@ const CodeEditor = forwardRef(
focus : ()=>viewRef.current.focus(), focus : ()=>viewRef.current.focus(),
})); }));
return <div className="codeEditor" ref={editorRef} style={style} />; return <div className='codeEditor' ref={editorRef} style={style} />;
}, },
); );