mirror of https://github.com/laurent22/joplin.git
pull/6823/head
parent
cfba73e938
commit
6e6275b1b7
|
@ -55,6 +55,24 @@ describe('markdownCommands', () => {
|
||||||
expect(editor.state.doc.toString()).toBe('Testing...');
|
expect(editor.state.doc.toString()).toBe('Testing...');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('for a cursor, bolding, then italicizing, should produce a bold-italic region', () => {
|
||||||
|
const initialDocText = '';
|
||||||
|
const editor = createEditor(
|
||||||
|
initialDocText, EditorSelection.cursor(0)
|
||||||
|
);
|
||||||
|
|
||||||
|
toggleBolded(editor);
|
||||||
|
toggleItalicized(editor);
|
||||||
|
expect(editor.state.doc.toString()).toBe('******');
|
||||||
|
|
||||||
|
editor.dispatch(editor.state.replaceSelection('Test'));
|
||||||
|
expect(editor.state.doc.toString()).toBe('***Test***');
|
||||||
|
|
||||||
|
toggleItalicized(editor);
|
||||||
|
editor.dispatch(editor.state.replaceSelection(' Test'));
|
||||||
|
expect(editor.state.doc.toString()).toBe('***Test*** Test');
|
||||||
|
});
|
||||||
|
|
||||||
it('toggling math should both create and navigate out of math regions', () => {
|
it('toggling math should both create and navigate out of math regions', () => {
|
||||||
const initialDocText = 'Testing... ';
|
const initialDocText = 'Testing... ';
|
||||||
const editor = createEditor(initialDocText, EditorSelection.cursor(initialDocText.length));
|
const editor = createEditor(initialDocText, EditorSelection.cursor(initialDocText.length));
|
||||||
|
|
|
@ -24,13 +24,58 @@ export const toggleBolded: Command = (view: EditorView): boolean => {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const toggleItalicized: Command = (view: EditorView): boolean => {
|
export const toggleItalicized: Command = (view: EditorView): boolean => {
|
||||||
const changes = toggleInlineFormatGlobally(view.state, {
|
let handledBoldItalicRegion = false;
|
||||||
nodeName: 'Emphasis',
|
|
||||||
|
|
||||||
template: { start: '*', end: '*' },
|
// Bold-italic regions' starting and ending patterns are similar to italicized regions.
|
||||||
matcher: { start: /[_*]/g, end: /[_*]/g },
|
// Thus, we need additional logic to convert bold regions to bold-italic regions.
|
||||||
});
|
view.dispatch(view.state.changeByRange((sel: SelectionRange) => {
|
||||||
view.dispatch(changes);
|
const changes: ChangeSpec[] = [];
|
||||||
|
|
||||||
|
// Only handle cursors (empty selections)
|
||||||
|
if (sel.empty) {
|
||||||
|
const doc = view.state.doc;
|
||||||
|
const selLine = doc.lineAt(sel.from);
|
||||||
|
|
||||||
|
const selStartLineIdx = sel.from - selLine.from;
|
||||||
|
const selEndLineIdx = sel.to - selLine.from;
|
||||||
|
const beforeSel = selLine.text.substring(0, selStartLineIdx);
|
||||||
|
const afterSel = selLine.text.substring(selEndLineIdx);
|
||||||
|
|
||||||
|
const isBolded = beforeSel.endsWith('**') && afterSel.startsWith('**');
|
||||||
|
|
||||||
|
// If at the end of a bold-italic region, exit the region.
|
||||||
|
if (afterSel.startsWith('***')) {
|
||||||
|
sel = EditorSelection.cursor(sel.to + 3);
|
||||||
|
handledBoldItalicRegion = true;
|
||||||
|
} else if (isBolded) {
|
||||||
|
// Create a bold-italic region.
|
||||||
|
changes.push({
|
||||||
|
from: sel.from,
|
||||||
|
to: sel.to,
|
||||||
|
insert: '**',
|
||||||
|
});
|
||||||
|
|
||||||
|
// Move to the center of the bold-italic region (**|**** -> ***|***)
|
||||||
|
sel = EditorSelection.cursor(sel.to + 1);
|
||||||
|
handledBoldItalicRegion = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
changes,
|
||||||
|
range: sel,
|
||||||
|
};
|
||||||
|
}));
|
||||||
|
|
||||||
|
if (!handledBoldItalicRegion) {
|
||||||
|
const changes = toggleInlineFormatGlobally(view.state, {
|
||||||
|
nodeName: 'Emphasis',
|
||||||
|
|
||||||
|
template: { start: '*', end: '*' },
|
||||||
|
matcher: { start: /[_*]/g, end: /[_*]/g },
|
||||||
|
});
|
||||||
|
view.dispatch(changes);
|
||||||
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue