mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-08-06 06:47:38 +00:00
adding comments, removing leftover code, renaming constant
This commit is contained in:
@@ -1,10 +1,12 @@
|
|||||||
/* eslint max-lines: ["error", { "max": 300 }] */
|
/* eslint max-lines: ["error", { "max": 300 }] */
|
||||||
import { keymap } from '@codemirror/view';
|
import { keymap } from '@codemirror/view';
|
||||||
import { undo, redo, indentMore, deleteLine } from '@codemirror/commands';
|
import { undo, redo, indentMore, indentLess, deleteLine } from '@codemirror/commands';
|
||||||
import { EditorSelection } from '@codemirror/state';
|
import { EditorSelection } from '@codemirror/state';
|
||||||
import { Prec } from '@codemirror/state';
|
import { Prec } from '@codemirror/state';
|
||||||
|
|
||||||
const insertTab = (view)=>{
|
const insertTab = (view)=>{
|
||||||
|
// If any selection spans multiple lines, delegates to CodeMirror's indentMore
|
||||||
|
// Otherwise inserts two spaces at each cursor/selection
|
||||||
const shouldIndent = view.state.selection.ranges.some((range)=>view.state.doc.lineAt(range.from).number !==
|
const shouldIndent = view.state.selection.ranges.some((range)=>view.state.doc.lineAt(range.from).number !==
|
||||||
view.state.doc.lineAt(range.to).number
|
view.state.doc.lineAt(range.to).number
|
||||||
);
|
);
|
||||||
@@ -12,26 +14,23 @@ const insertTab = (view)=>{
|
|||||||
if(shouldIndent) return indentMore(view);
|
if(shouldIndent) return indentMore(view);
|
||||||
|
|
||||||
const changes = [];
|
const changes = [];
|
||||||
const ranges = [];
|
|
||||||
|
|
||||||
for (const range of view.state.selection.ranges) {
|
for (const range of view.state.selection.ranges) {
|
||||||
changes.push({
|
changes.push({
|
||||||
from : range.from,
|
from : range.from,
|
||||||
to : range.to,
|
to : range.to,
|
||||||
insert : ' '
|
insert : ' ' // Insert two spaces, not a tab char!
|
||||||
});
|
|
||||||
|
|
||||||
ranges.push({
|
|
||||||
anchor : range.from
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
const tr = view.state.update({ changes });
|
// Create a transaction so we can map old positions to
|
||||||
|
// their new positions after the edits are applied
|
||||||
|
const mappedChanges = view.state.update({ changes });
|
||||||
|
|
||||||
view.dispatch({
|
view.dispatch({
|
||||||
changes,
|
changes,
|
||||||
selection : EditorSelection.create(
|
selection : EditorSelection.create(
|
||||||
view.state.selection.ranges.map((range)=>EditorSelection.cursor(
|
view.state.selection.ranges.map((range)=>EditorSelection.cursor(
|
||||||
tr.changes.mapPos(range.from, 1) + 2
|
mappedChanges.changes.mapPos(range.from, 1) + 2
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
@@ -40,20 +39,6 @@ const insertTab = (view)=>{
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const indentLess = (view)=>{
|
|
||||||
const { from, to } = view.state.selection.main;
|
|
||||||
const lines = [];
|
|
||||||
for (let l = view.state.doc.lineAt(from).number; l <= view.state.doc.lineAt(to).number; l++) {
|
|
||||||
const line = view.state.doc.line(l);
|
|
||||||
const match = line.text.match(/^ {1,2}/); // match up to 2 spaces
|
|
||||||
if(match) {
|
|
||||||
lines.push({ from: line.from, to: line.from + match[0].length, insert: '' });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(lines.length > 0) view.dispatch({ changes: lines });
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
const wrapSelection = (prefix, suffix)=>(view)=>{
|
const wrapSelection = (prefix, suffix)=>(view)=>{
|
||||||
const changes = [];
|
const changes = [];
|
||||||
|
|
||||||
@@ -195,13 +180,12 @@ const newPage = (view)=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
export const generalKeymap = Prec.high(keymap.of([
|
export const generalKeymap = Prec.high(keymap.of([
|
||||||
{ key: 'Tab', run: insertTab },
|
{ key: 'Tab', run: insertTab }, //runs indentMore if multiple lines selected in a single selection
|
||||||
//{ key: 'Shift-Tab', run: indentMore },
|
{ key: 'Shift-Tab', run: indentLess },
|
||||||
{ key: 'Shift-Tab', run: indentLess },
|
{ key: 'Mod-z', run: undo }, //it may be unnecessary
|
||||||
{ key: 'Mod-z', run: undo }, //i think it may be unnecessary
|
|
||||||
{ key: 'Mod-Shift-z', run: redo },
|
{ key: 'Mod-Shift-z', run: redo },
|
||||||
{ key: 'Mod-y', run: redo },
|
{ key: 'Mod-y', run: redo }, //user asked, so double keybind
|
||||||
{ key: 'Mod-d', run: deleteLine },
|
{ key: 'Mod-d', run: deleteLine }, //annoyingly overrides "selectNextOccurrence" because users asked
|
||||||
]));
|
]));
|
||||||
|
|
||||||
export const markdownKeymap = Prec.highest(keymap.of([
|
export const markdownKeymap = Prec.highest(keymap.of([
|
||||||
|
|||||||
Reference in New Issue
Block a user