mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-09-28 04:32:58 +00:00
makelist work
This commit is contained in:
@@ -6,6 +6,9 @@ import { Prec } from '@codemirror/state';
|
|||||||
import * as prettier from 'prettier/standalone';
|
import * as prettier from 'prettier/standalone';
|
||||||
import * as postcssPlugin from 'prettier/plugins/postcss';
|
import * as postcssPlugin from 'prettier/plugins/postcss';
|
||||||
|
|
||||||
|
|
||||||
|
import { ChangeSet } from '@codemirror/state';
|
||||||
|
|
||||||
export async function formatCSS(view) {
|
export async function formatCSS(view) {
|
||||||
try {
|
try {
|
||||||
const { from, to, empty } = view.state.selection.main;
|
const { from, to, empty } = view.state.selection.main;
|
||||||
@@ -65,8 +68,7 @@ const forEachSelection = (view, fn)=>{
|
|||||||
const insertTab = (view)=>{
|
const insertTab = (view)=>{
|
||||||
// If any selection spans multiple lines, delegates to CodeMirror's indentMore
|
// If any selection spans multiple lines, delegates to CodeMirror's indentMore
|
||||||
// Otherwise inserts two spaces at each cursor/selection
|
// Otherwise inserts two spaces at each cursor/selection
|
||||||
const shouldIndent = view.state.selection.ranges.some((range)=>
|
const shouldIndent = view.state.selection.ranges.some((range)=>view.state.doc.lineAt(range.from).number !==
|
||||||
view.state.doc.lineAt(range.from).number !==
|
|
||||||
view.state.doc.lineAt(range.to).number
|
view.state.doc.lineAt(range.to).number
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -246,33 +248,49 @@ const makeLink = (view)=>{
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
const makeList = (type)=>(view)=>{
|
|
||||||
const { from, to } = view.state.selection.main;
|
|
||||||
const startLine = view.state.doc.lineAt(from);
|
|
||||||
const endLine = view.state.doc.lineAt(to);
|
|
||||||
const lines = [];
|
|
||||||
|
|
||||||
for (let lineNo = startLine.number; lineNo <= endLine.number; lineNo++) {
|
const makeList = (prefix)=>(view)=>forEachSelection(view, (state, selectionRange)=>{
|
||||||
lines.push(view.state.doc.line(lineNo).text);
|
const { from, to } = selectionRange;
|
||||||
|
|
||||||
|
const startLine = state.doc.lineAt(from);
|
||||||
|
const endLine = state.doc.lineAt(to);
|
||||||
|
|
||||||
|
const selectedLines = [];
|
||||||
|
|
||||||
|
for (
|
||||||
|
let lineNumber = startLine.number;
|
||||||
|
lineNumber <= endLine.number;
|
||||||
|
lineNumber++
|
||||||
|
) {
|
||||||
|
selectedLines.push(state.doc.line(lineNumber));
|
||||||
}
|
}
|
||||||
const joined = lines.join('\n');
|
|
||||||
|
|
||||||
const newText = type === 'UL'
|
const allLinesHavePrefix = selectedLines.every((line)=>line.text.startsWith(prefix)
|
||||||
? joined.replace(/^/gm, '- ')
|
);
|
||||||
: joined.replace(/^/gm, (_, offset)=>{
|
|
||||||
const lineNumber = joined.slice(0, offset).split('\n').length;
|
|
||||||
return `${lineNumber}. `;
|
|
||||||
});
|
|
||||||
|
|
||||||
view.dispatch({
|
const changes = selectedLines.map((line)=>{
|
||||||
changes : {
|
const shouldRemovePrefix =
|
||||||
from : startLine.from,
|
allLinesHavePrefix && line.text.startsWith(prefix);
|
||||||
to : endLine.to,
|
|
||||||
insert : newText
|
return {
|
||||||
}
|
from : line.from,
|
||||||
});
|
to : shouldRemovePrefix
|
||||||
return true;
|
? line.from + prefix.length
|
||||||
|
: line.from,
|
||||||
|
insert : shouldRemovePrefix ? '' : prefix,
|
||||||
};
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
const changeSet = ChangeSet.of(changes, state.doc.length);
|
||||||
|
|
||||||
|
return {
|
||||||
|
changes,
|
||||||
|
range : EditorSelection.range(
|
||||||
|
changeSet.mapPos(from, -1),
|
||||||
|
changeSet.mapPos(to, 1)
|
||||||
|
),
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
const makeHeader = (level)=>(view)=>forEachSelection(view, (state, range)=>{
|
const makeHeader = (level)=>(view)=>forEachSelection(view, (state, range)=>{
|
||||||
const { from, to } = range;
|
const { from, to } = range;
|
||||||
@@ -332,8 +350,8 @@ export const markdownKeymap = Prec.highest(keymap.of([
|
|||||||
{ key: 'Shift-Mod-m', run: wrapSelection('{{\n', '\n}}') },
|
{ key: 'Shift-Mod-m', run: wrapSelection('{{\n', '\n}}') },
|
||||||
{ key: 'Mod-/', run: wrapSelection('<!-- ', ' -->') },
|
{ key: 'Mod-/', run: wrapSelection('<!-- ', ' -->') },
|
||||||
{ key: 'Mod-k', run: makeLink },
|
{ key: 'Mod-k', run: makeLink },
|
||||||
{ key: 'Mod-Shift-u', run: makeList('UL') },
|
{ key: 'Mod-Shift-u', run: makeList('- ') },
|
||||||
{ key: 'Mod-Shift-o', run: makeList('OL') },
|
{ key: 'Mod-Shift-o', run: makeList('1. ') },
|
||||||
{ key: 'Shift-Mod-1', run: makeHeader(1) },
|
{ key: 'Shift-Mod-1', run: makeHeader(1) },
|
||||||
{ key: 'Shift-Mod-2', run: makeHeader(2) },
|
{ key: 'Shift-Mod-2', run: makeHeader(2) },
|
||||||
{ key: 'Shift-Mod-3', run: makeHeader(3) },
|
{ key: 'Shift-Mod-3', run: makeHeader(3) },
|
||||||
|
|||||||
Reference in New Issue
Block a user