0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-05-07 18:48:39 +00:00

repair shortcuts to put the cursor at the center if there is no selection

This commit is contained in:
Víctor Losada Hernández
2026-04-22 17:32:26 +02:00
parent f863871173
commit 7139993e15
+96 -26
View File
@@ -20,72 +20,142 @@ const indentLess = (view)=>{
const makeBold = (view)=>{ const makeBold = (view)=>{
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 text = selected.startsWith('**') && selected.endsWith('**')
? selected.slice(2, -2) let text, cursor;
: `**${selected}**`;
if(from === to) {
text = '****';
cursor = from + 2;
} else if(selected.startsWith('**') && selected.endsWith('**')) {
text = selected.slice(2, -2);
cursor = from + text.length;
} else {
text = `**${selected}**`;
cursor = from + text.length;
}
view.dispatch({ view.dispatch({
changes : { from, to, insert: text }, changes : { from, to, insert: text },
selection : { anchor: from + text.length }, selection : { anchor: cursor },
}); });
return true; return true;
}; };
const makeItalic = (view)=>{ const makeItalic = (view)=>{
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 text = selected.startsWith('*') && selected.endsWith('*')
? selected.slice(1, -1) let text, cursor;
: `*${selected}*`;
if(from === to) {
text = '**';
cursor = from + 1;
} else if(selected.startsWith('*') && selected.endsWith('*')) {
text = selected.slice(2, -2);
cursor = from + text.length;
} else {
text = `*${selected}*`;
cursor = from + text.length;
}
view.dispatch({ view.dispatch({
changes : { from, to, insert: text }, changes : { from, to, insert: text },
selection : { anchor: from + text.length }, selection : { anchor: cursor },
}); });
return true; return true;
}; };
const makeUnderline = (view)=>{ const makeUnderline = (view)=>{
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 text = selected.startsWith('<u>') && selected.endsWith('</u>')
? selected.slice(3, -4) let text, cursor;
: `<u>${selected}</u>`;
if(from === to) {
text = '<u></u>';
cursor = from + 3;
} else if(selected.startsWith('<u>') && selected.endsWith('</u>')) {
text = selected.slice(3, -4);
cursor = from + text.length;
} else {
text = `<u>${selected}</u>`;
cursor = from + text.length;
}
view.dispatch({ view.dispatch({
changes : { from, to, insert: text }, changes : { from, to, insert: text },
selection : { anchor: from + text.length }, selection : { anchor: cursor },
}); });
return true; return true;
}; };
const makeSuper = (view)=>{ const makeSuper = (view)=>{
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 text = selected.startsWith('^') && selected.endsWith('^')
? selected.slice(1, -1) let text, cursor;
: `^${selected}^`;
if(from === to) {
text = '^^';
cursor = from + 1;
} else if(selected.startsWith('^') && selected.endsWith('^')) {
text = selected.slice(1, -1);
cursor = from + text.length;
} else {
text = `^${selected}^`;
cursor = from + text.length;
}
view.dispatch({ view.dispatch({
changes : { from, to, insert: text }, changes : { from, to, insert: text },
selection : { anchor: from + text.length }, selection : { anchor: cursor },
}); });
return true; return true;
}; };
const makeSub = (view)=>{ const makeSub = (view)=>{
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 text = selected.startsWith('^^') && selected.endsWith('^^')
? selected.slice(2, -2) let text, cursor;
: `^^${selected}^^`;
if(from === to) {
text = '^^^^';
cursor = from + 2;
} else if(selected.startsWith('^^') && selected.endsWith('^^')) {
text = selected.slice(2, -2);
cursor = from + text.length;
} else {
text = `^^${selected}^^`;
cursor = from + text.length;
}
view.dispatch({ view.dispatch({
changes : { from, to, insert: text }, changes : { from, to, insert: text },
selection : { anchor: from + text.length }, selection : { anchor: cursor },
}); });
return true; return true;
}; };
const makeNbsp = (view)=>{ const makeNbsp = (view) => {
const { from, to } = view.state.selection.main; const { from } = view.state.selection.main;
view.dispatch({ changes: { from, to, insert: '&nbsp;' } });
return true; const prev2 = from >= 2
? view.state.doc.sliceString(from - 2, from)
: '';
const insert = (prev2 === ':>' || prev2 === '>>') ? '>' : ':>';
view.dispatch({
changes: { from, to: from, insert },
selection: { anchor: from + insert.length },
});
return true;
}; };
const makeSpace = (view)=>{ const makeSpace = (view)=>{
@@ -193,7 +263,7 @@ export const generalKeymap = Prec.high(keymap.of([
{ key: 'Mod-z', run: undo }, //i think 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 },
{ key: 'Mod-d', run: deleteLine}, { key: 'Mod-d', run: deleteLine },
])); ]));
export const markdownKeymap = Prec.highest(keymap.of([ export const markdownKeymap = Prec.highest(keymap.of([