last fixes

This commit is contained in:
Víctor Losada Hernández
2026-09-27 22:50:42 +02:00
parent a39b86edf0
commit 3ec85b6f52
@@ -57,12 +57,11 @@ export async function formatCSS(view) {
return true; return true;
} }
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)));
return true; return true;
}; };
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
@@ -165,7 +164,7 @@ const wrapSelection = (prefix, suffix)=>(view)=>{
return true; return true;
}; };
const makeNbsp = (view) => forEachSelection(view, (state, range) => { const makeNbsp = (view)=>forEachSelection(view, (state, range)=>{
const { from, to } = range; const { from, to } = range;
const prev2 = from >= 2 ? state.doc.sliceString(from - 2, from) : ''; const prev2 = from >= 2 ? state.doc.sliceString(from - 2, from) : '';
@@ -177,7 +176,7 @@ const makeNbsp = (view) => forEachSelection(view, (state, range) => {
}; };
}); });
const makeSpace = (view) => forEachSelection(view, (state, range) => { const makeSpace = (view)=>forEachSelection(view, (state, range)=>{
const { from, to } = range; const { from, to } = range;
const selected = state.doc.sliceString(from, to); const selected = state.doc.sliceString(from, to);
const match = selected.match(/^{{width:(\d+)% }}$/); const match = selected.match(/^{{width:(\d+)% }}$/);
@@ -195,23 +194,23 @@ const makeSpace = (view) => forEachSelection(view, (state, range) => {
}; };
}); });
const removeSpace = (view) => forEachSelection(view, (state, range) => { const removeSpace = (view)=>forEachSelection(view, (state, range)=>{
const { from, to } = view.state.selection.main; const { from, to } = view.state.selection.main;
const selected = view.state.doc.sliceString(from, to); const selected = view.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 newText = percent > 0 ? `{{width:${percent}% }}` : ''; const newText = percent > 0 ? `{{width:${percent}% }}` : '';
return{ changes: { from, to, insert: newText } }; return { changes: { from, to, insert: newText } };
} }
return true; return true;
}); });
const makeLink = (view) => { const makeLink = (view)=>{
const ranges = view.state.selection.ranges; const ranges = view.state.selection.ranges;
// Multiple selections: pair them as alt text + URL // Multiple selections: pair them as alt text + URL
if (ranges.length > 1) { if(ranges.length > 1) {
const changes = []; const changes = [];
for (let i = 0; i < ranges.length - 1; i += 2) { for (let i = 0; i < ranges.length - 1; i += 2) {
@@ -226,9 +225,9 @@ const makeLink = (view) => {
).trim(); ).trim();
changes.push({ changes.push({
from: ranges[i].from, from : ranges[i].from,
to: ranges[i + 1].to, to : ranges[i + 1].to,
insert: `[${alt}](${url})` insert : `[${alt}](${url})`
}); });
} }
@@ -244,14 +243,14 @@ const makeLink = (view) => {
let text; let text;
if (isLink) { if(isLink) {
text = `${isLink[1]} ${isLink[2]}`; text = `${isLink[1]} ${isLink[2]}`;
} else { } else {
const isUrl = const isUrl =
/^(https?:\/\/|www\.)\S+$/i.test(selected) || /^(https?:\/\/|www\.)\S+$/i.test(selected) ||
/^[a-z0-9-]+(\.[a-z0-9-]+)+([/?#]\S*)?$/i.test(selected); /^[a-z0-9-]+(\.[a-z0-9-]+)+([/?#]\S*)?$/i.test(selected);
if (isUrl) { if(isUrl) {
const url = selected; const url = selected;
const domain = url const domain = url
.replace(/^https?:\/\//i, '') .replace(/^https?:\/\//i, '')
@@ -267,7 +266,7 @@ const makeLink = (view) => {
} }
view.dispatch({ view.dispatch({
changes: { from, to, insert: text } changes : { from, to, insert: text }
}); });
return true; return true;
@@ -301,38 +300,35 @@ const makeList = (type)=>(view)=>{
return true; return true;
}; };
const makeHeader = (level)=>(view)=>{ const makeHeader = (level)=>(view)=>forEachSelection(view, (state, range)=>{
const { from, to } = view.state.selection.main; const { from, to } = range;
const selected = view.state.doc.sliceString(from, to); const selected = state.doc.sliceString(from, to);
const text = `${'#'.repeat(level)} ${selected}`; const insert = `${'#'.repeat(level)} ${selected}`;
if(selected.length === 0) { if(selected.length === 0) {
view.dispatch({ changes: { from, to, insert: text }, selection: { anchor: from + text.length } }); return { changes: { from, to, insert: insert }, range: EditorSelection.cursor(from + insert.length) };
} else { } else {
view.dispatch({ changes: { from, to, insert: text } }); return { changes: { from, to, insert: insert }, range};
} }
});
return true; const newColumn = (view)=>forEachSelection(view, (state, range)=>{
}; const { from, to } = range;
const newColumn = (view)=>{
const { from, to } = view.state.selection.main;
const insert = '\n\\column\n\n' ; const insert = '\n\\column\n\n' ;
view.dispatch({ return {
changes : { from, to, insert }, changes : { from, to, insert },
selection : { anchor: from + insert.length } range: EditorSelection.cursor(from + insert.length)
}); };
return true; });
};
const newPage = (view)=>{ const newPage = (view)=>forEachSelection(view, (state, range)=>{
const { from, to } = view.state.selection.main; const { from, to } = range;
const insert = '\n\\page\n\n'; const insert = '\n\\page\n\n';
view.dispatch({
changes : { from, to, insert }, return {
selection : { anchor: from + insert.length } changes: { from, to, insert },
}); range: EditorSelection.cursor(from + insert.length)
return true; };
}; });
export const generalKeymap = Prec.high(keymap.of([ export const generalKeymap = Prec.high(keymap.of([
{ key: 'Tab', run: insertTab }, //runs indentMore if multiple lines selected in a single selection { key: 'Tab', run: insertTab }, //runs indentMore if multiple lines selected in a single selection