move from to and selected to foreachselection

This commit is contained in:
Víctor Losada Hernández
2026-09-28 23:17:10 +02:00
parent db356dcc53
commit cf17437603
@@ -55,7 +55,7 @@ export async function formatCSS(view) {
} }
const forEachSelection = (view, fn)=>{ const forEachSelection = (view, fn)=>{
view.dispatch(view.state.changeByRange((range)=>fn(view.state, range))); view.dispatch(view.state.changeByRange((range)=>fn(view.state, range.from, range.to, view.state.doc.sliceString(range.from, range.to))));
return true; return true;
}; };
@@ -67,8 +67,7 @@ const insertTab = (view)=>{
); );
if(shouldIndent) return indentMore(view); if(shouldIndent) return indentMore(view);
return forEachSelection(view, (state, range)=>{ return forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = range;
return { return {
changes : { from, to, insert: ' ' }, // Insert two spaces, not a tab char! changes : { from, to, insert: ' ' }, // Insert two spaces, not a tab char!
range : EditorSelection.cursor(from + 2) range : EditorSelection.cursor(from + 2)
@@ -76,10 +75,7 @@ const insertTab = (view)=>{
}); });
}; };
const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection(view, (state, range)=>{ const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = range;
const selected = state.doc.sliceString(from, to);
const noSelection = from === to; const noSelection = from === to;
if(noSelection) { if(noSelection) {
const insert = prefix + suffix; const insert = prefix + suffix;
@@ -107,9 +103,7 @@ const wrapSelection = (prefix, suffix)=>(view)=>forEachSelection(view, (state, r
}; };
}); });
const makeNbsp = (view)=>forEachSelection(view, (state, range)=>{ const makeNbsp = (view)=>forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = range;
const prev2 = from >= 2 ? state.doc.sliceString(from - 2, from) : ''; const prev2 = from >= 2 ? state.doc.sliceString(from - 2, from) : '';
const insert = (prev2 === ':>' || prev2 === '>>') ? '>' : ':>'; const insert = (prev2 === ':>' || prev2 === '>>') ? '>' : ':>';
@@ -119,10 +113,7 @@ const makeNbsp = (view)=>forEachSelection(view, (state, range)=>{
}; };
}); });
const makeSpace = (view)=>forEachSelection(view, (state, range)=>{ const makeSpace = (view)=>forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = range;
const selected = state.doc.sliceString(from, to);
let insert = '{{width:10% }}'; let insert = '{{width:10% }}';
const match = selected.match(/^{{width:(\d+)% }}$/); const match = selected.match(/^{{width:(\d+)% }}$/);
if(match) { if(match) {
@@ -135,21 +126,17 @@ const makeSpace = (view)=>forEachSelection(view, (state, range)=>{
}; };
}); });
const removeSpace = (view)=>forEachSelection(view, (state, range)=>{ const removeSpace = (view)=>forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = range;
const selected = state.doc.sliceString(from, to);
const match = selected.match(/^{{width:(\d+)% }}$/); const match = selected.match(/^{{width:(\d+)% }}$/);
if(match) { if(match) {
const percent = parseInt(match[1], 10) - 10; const percent = parseInt(match[1], 10) - 10;
const insert = percent > 0 ? `{{width:${percent}% }}` : ''; const insert = percent > 0 ? `{{width:${percent}% }}` : '';
return { changes: { from, to, insert: insert } }; if(insert.length === 0) return { changes: { from, to, insert: insert }, range: EditorSelection.cursor(from) };
else return { changes: { from, to, insert: insert }, range };
} }
}); });
const makeLink = (view)=>forEachSelection(view, (state, range)=>{ const makeLink = (view)=>forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = range;
const selected = state.doc.sliceString(from, to).trim();
// If the selection is already a Markdown link, unwrap it // If the selection is already a Markdown link, unwrap it
const existingLink = /^\[(.*)\]\((.*)\)$/.exec(selected); const existingLink = /^\[(.*)\]\((.*)\)$/.exec(selected);
if(existingLink) { if(existingLink) {
@@ -190,9 +177,7 @@ const makeLink = (view)=>forEachSelection(view, (state, range)=>{
}; };
}); });
const addPrefixAtLineStart = (prefix)=>(view)=>forEachSelection(view, (state, selectionRange)=>{ const addPrefixAtLineStart = (prefix)=>(view)=>forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = selectionRange;
const startLine = state.doc.lineAt(from); const startLine = state.doc.lineAt(from);
const endLine = state.doc.lineAt(to); const endLine = state.doc.lineAt(to);
const selectedLines = []; const selectedLines = [];
@@ -223,8 +208,7 @@ const addPrefixAtLineStart = (prefix)=>(view)=>forEachSelection(view, (state, se
}; };
}); });
const newBreak = (type)=>(view)=>forEachSelection(view, (state, range)=>{ const newBreak = (type)=>(view)=>forEachSelection(view, (state, from, to, selected)=>{
const { from, to } = range;
const insert = `\n\\${type}\n\n`; const insert = `\n\\${type}\n\n`;
return { return {
changes : { from, to, insert }, changes : { from, to, insert },