Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto format list on TAB #2827

Merged
merged 2 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,31 @@ export class AutoFormatPlugin implements EditorPlugin {
unlink(editor, rawEvent);
}
break;
case 'Tab':
formatTextSegmentBeforeSelectionMarker(
editor,
(model, _previousSegment, paragraph, _markerFormat, context) => {
const { autoBullet, autoNumbering } = this.options;
let shouldList = false;
if (autoBullet || autoNumbering) {
shouldList = keyboardListTrigger(
model,
paragraph,
context,
autoBullet,
autoNumbering
);
context.canUndoByBackspace = shouldList;
event.rawEvent.preventDefault();
}

return shouldList;
},
{
changeSource: ChangeSource.AutoFormat,
apiName: 'autoToggleList',
}
);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,166 @@ describe('Content Model Auto Format Plugin Test', () => {
});
});

describe('onPluginEvent - [TAB] - keyboardListTrigger', () => {
function runTest(
event: KeyDownEvent,
testBullet: boolean,
expectResult: boolean,
options?: AutoFormatOptions
) {
const plugin = new AutoFormatPlugin(options);
plugin.initialize(editor);

plugin.onPluginEvent(event);

const formatOptions = {
apiName: '',
};

const inputModel = (bullet: boolean): ContentModelDocument => ({
blockGroupType: 'Document',
blocks: [
{
blockType: 'Paragraph',
segments: [
{
segmentType: 'Text',
text: bullet ? '*' : '1)',
format: {},
},
{
segmentType: 'SelectionMarker',
isSelected: true,
format: {},
},
],
format: {},
},
],
format: {},
});

formatTextSegmentBeforeSelectionMarkerSpy.and.callFake((editor, callback, options) => {
expect(callback).toBe(
editor,
(
_model: ContentModelDocument,
_previousSegment: ContentModelText,
paragraph: ContentModelParagraph,
context: FormatContentModelContext
) => {
const result = keyboardListTrigger(
inputModel(testBullet),
paragraph,
context,
options!.autoBullet,
options!.autoNumbering
);
expect(result).toBe(expectResult);
const preventDefaultSpy = spyOn(event.rawEvent, 'preventDefault');
if (result) {
expect(context.canUndoByBackspace).toBe(true);
expect(preventDefaultSpy).toHaveBeenCalled();
} else {
expect(context.canUndoByBackspace).toBe(false);
expect(preventDefaultSpy).not.toHaveBeenCalled();
}
formatOptions.apiName = result ? 'autoToggleList' : '';
return result;
}
);
expect(options).toEqual({
changeSource: 'AutoFormat',
apiName: formatOptions.apiName,
});
});
}

it('[TAB] should trigger keyboardListTrigger bullet ', () => {
const event: KeyDownEvent = {
eventType: 'keyDown',
rawEvent: {
key: 'Tab',
defaultPrevented: false,
handledByEditFeature: false,
} as any,
};
runTest(event, true, true, { autoBullet: true, autoNumbering: false });
});

it('[TAB] should trigger keyboardListTrigger numbering ', () => {
const event: KeyDownEvent = {
eventType: 'keyDown',
rawEvent: {
key: 'Tab',
defaultPrevented: false,
handledByEditFeature: false,
} as any,
};
runTest(event, false, true, { autoBullet: true, autoNumbering: true });
});

it('[TAB] should not trigger keyboardListTrigger numbering ', () => {
const event: KeyDownEvent = {
eventType: 'keyDown',
rawEvent: {
key: 'Tab',
defaultPrevented: false,
handledByEditFeature: false,
} as any,
};
runTest(event, false, false, { autoBullet: true, autoNumbering: false });
});

it('[TAB] should not trigger keyboardListTrigger bullet ', () => {
const event: KeyDownEvent = {
eventType: 'keyDown',
rawEvent: {
key: 'Tab',
defaultPrevented: false,
handledByEditFeature: false,
} as any,
};
runTest(event, true, false, { autoBullet: false, autoNumbering: false });
});

it('[TAB] should not trigger keyboardListTrigger - not tab ', () => {
const event: KeyDownEvent = {
eventType: 'keyDown',
rawEvent: {
key: 'Ctrl',
defaultPrevented: false,
handledByEditFeature: false,
} as any,
};
runTest(event, true, false, { autoBullet: true, autoNumbering: true });
});

it('[TAB] should not trigger keyboardListTrigger - default prevented ', () => {
const event: KeyDownEvent = {
eventType: 'keyDown',
rawEvent: {
key: 'Tab',
defaultPrevented: true,
handledByEditFeature: false,
} as any,
};
runTest(event, true, false, { autoBullet: true, autoNumbering: true });
});

it('[TAB] should not trigger keyboardListTrigger - handledByEditFeature', () => {
const event: KeyDownEvent = {
eventType: 'keyDown',
rawEvent: {
key: 'Tab',
defaultPrevented: false,
handledByEditFeature: true,
} as any,
};
runTest(event, true, false, { autoBullet: true, autoNumbering: true });
});
});

describe('onPluginEvent - createLink', () => {
let createLinkSpy: jasmine.Spy;

Expand Down
Loading