mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-03-28 14:28:12 +00:00
multiple cursors
This commit is contained in:
@@ -11,7 +11,8 @@ import {
|
|||||||
scrollPastEnd,
|
scrollPastEnd,
|
||||||
Decoration,
|
Decoration,
|
||||||
ViewPlugin,
|
ViewPlugin,
|
||||||
WidgetType
|
WidgetType,
|
||||||
|
drawSelection,
|
||||||
} from '@codemirror/view';
|
} from '@codemirror/view';
|
||||||
import { EditorState, Compartment } from '@codemirror/state';
|
import { EditorState, Compartment } from '@codemirror/state';
|
||||||
import { foldGutter, foldKeymap, syntaxHighlighting } from '@codemirror/language';
|
import { foldGutter, foldKeymap, syntaxHighlighting } from '@codemirror/language';
|
||||||
@@ -37,11 +38,11 @@ import { legacyCustomHighlightStyle, legacyTokenizeCustomMarkdown } from './lega
|
|||||||
const createHighlightPlugin = (renderer, tab)=>{
|
const createHighlightPlugin = (renderer, tab)=>{
|
||||||
let tokenize;
|
let tokenize;
|
||||||
|
|
||||||
if (tab === "brewStyles") {
|
if(tab === 'brewStyles') {
|
||||||
tokenize = tokenizeCustomCSS;
|
tokenize = tokenizeCustomCSS;
|
||||||
} else {
|
} else {
|
||||||
tokenize = renderer === 'V3' ? tokenizeCustomMarkdown : legacyTokenizeCustomMarkdown;
|
tokenize = renderer === 'V3' ? tokenizeCustomMarkdown : legacyTokenizeCustomMarkdown;
|
||||||
}
|
}
|
||||||
/* eslint-disable no-restricted-syntax */
|
/* eslint-disable no-restricted-syntax */
|
||||||
class countWidget extends WidgetType {
|
class countWidget extends WidgetType {
|
||||||
constructor(count) {
|
constructor(count) {
|
||||||
@@ -187,7 +188,9 @@ const CodeEditor = forwardRef(
|
|||||||
...(tab !== 'brewStyles' ? [autocompleteEmoji] : []),
|
...(tab !== 'brewStyles' ? [autocompleteEmoji] : []),
|
||||||
search(),
|
search(),
|
||||||
keymap.of([...defaultKeymap, foldKeymap, ...searchKeymap]),
|
keymap.of([...defaultKeymap, foldKeymap, ...searchKeymap]),
|
||||||
customKeymap
|
customKeymap,
|
||||||
|
drawSelection(),
|
||||||
|
EditorState.allowMultipleSelections.of(true),
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -282,16 +285,23 @@ const CodeEditor = forwardRef(
|
|||||||
|
|
||||||
injectText : (text)=>{
|
injectText : (text)=>{
|
||||||
const view = viewRef.current;
|
const view = viewRef.current;
|
||||||
const { from, to } = view.state.selection.main;
|
const changes = view.state.selection.ranges.map((range)=>({
|
||||||
|
from : range.from,
|
||||||
|
to : range.to,
|
||||||
|
insert : text
|
||||||
|
}));
|
||||||
|
|
||||||
|
const newRanges = view.state.selection.ranges.map((range)=>({
|
||||||
|
anchor : range.from + text.length
|
||||||
|
}));
|
||||||
|
|
||||||
view.dispatch({
|
view.dispatch({
|
||||||
changes : { from, to, insert: text },
|
changes,
|
||||||
selection : { anchor: from + text.length },
|
selection : { ranges: newRanges }
|
||||||
});
|
});
|
||||||
|
|
||||||
view.focus();
|
view.focus();
|
||||||
},
|
},
|
||||||
|
|
||||||
getCursorPosition : ()=>viewRef.current.state.selection.main.head,
|
getCursorPosition : ()=>viewRef.current.state.selection.main.head,
|
||||||
|
|
||||||
getScrollTop : ()=>viewRef.current.scrollDOM.scrollTop,
|
getScrollTop : ()=>viewRef.current.scrollDOM.scrollTop,
|
||||||
|
|||||||
@@ -19,8 +19,7 @@ const customTags = {
|
|||||||
|
|
||||||
//CSS
|
//CSS
|
||||||
|
|
||||||
variable: 'variable',
|
variable : 'variable',
|
||||||
colorMark: 'colorMark',
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export function tokenizeCustomMarkdown(text) {
|
export function tokenizeCustomMarkdown(text) {
|
||||||
|
|||||||
@@ -2,14 +2,12 @@
|
|||||||
import { keymap } from '@codemirror/view';
|
import { keymap } from '@codemirror/view';
|
||||||
import { undo, redo } from '@codemirror/commands';
|
import { undo, redo } from '@codemirror/commands';
|
||||||
|
|
||||||
const insertTabAtCursor = (view) => {
|
const insertTabAtCursor = (view)=>{
|
||||||
const { from } = view.state.selection.main;
|
const { from } = view.state.selection.main;
|
||||||
|
|
||||||
view.dispatch({
|
view.dispatch({
|
||||||
changes: { from, insert: ' ' },
|
changes : { from, insert: ' ' },
|
||||||
selection: { anchor: from + 1 }
|
selection : { anchor: from + 1 }
|
||||||
});
|
});
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user