Skip to content

Commit

Permalink
Merge pull request #2227 from microsoft/u/juliaroldi/new-auto-hyphen
Browse files Browse the repository at this point in the history
Auto format - Auto format hyphen
  • Loading branch information
juliaroldi authored Nov 29, 2023
2 parents 13afd41 + deed711 commit 6b02a3d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,19 @@ export default class AutoFormat implements EditorPlugin {
if (
this.lastKeyTyped === '-' &&
!specialCharacters.test(keyTyped) &&
keyTyped !== ' ' &&
keyTyped !== '-'
) {
const searcher = this.editor.getContentSearcherOfCursor(event);
const textBeforeCursor = searcher?.getSubStringBefore(3);
const dashes = searcher?.getSubStringBefore(2);
const isPrecededByADash = textBeforeCursor?.[0] === '-';
const isPrecededByASpace = textBeforeCursor?.[0] === ' ';
const isSpaced =
(textBeforeCursor == ' --' && keyTyped !== ' ') ||
(textBeforeCursor !== ' --' && keyTyped === ' ');

if (
isPrecededByADash ||
isPrecededByASpace ||
isSpaced ||
(typeof textBeforeCursor === 'string' &&
specialCharacters.test(textBeforeCursor[0])) ||
dashes !== '--'
Expand All @@ -78,7 +80,10 @@ export default class AutoFormat implements EditorPlugin {
}

const textRange = searcher?.getRangeFromText(dashes, true /* exactMatch */);
const nodeHyphen = document.createTextNode('—');
const nodeHyphen =
textBeforeCursor === ' --' && keyTyped === ' '
? document.createTextNode('–')
: document.createTextNode('—');
this.editor.addUndoSnapshot(
() => {
if (textRange) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ describe('AutoHyphen |', () => {
plugin.onPluginEvent(keyDown(keysTyped[1]));
plugin.onPluginEvent(keyDown(keysTyped[2]));
plugin.onPluginEvent(keyDown(keysTyped[3]));
plugin.onPluginEvent(keyDown(keysTyped[4]));
plugin.onPluginEvent(keyDown(keysTyped[5]));
plugin.onPluginEvent(keyDown(keysTyped[6]));
expect(editor.getContent()).toBe(expectedResult);
}

Expand All @@ -45,6 +48,14 @@ describe('AutoHyphen |', () => {
);
});

it('Should format with space ', () => {
runTestShouldHandleAutoHyphen(
'<div>t--</div><!--{"start":[0,0,4],"end":[0,0,4]}-->',
['t', ' ', '-', '-', ' ', 'b'],
'<div>t—</div>'
);
});

it('Should not format| - ', () => {
runTestShouldHandleAutoHyphen(
'<div>t—-</div><!--{"start":[0,0,3],"end":[0,0,3]}-->',
Expand Down

0 comments on commit 6b02a3d

Please sign in to comment.