Skip to content
Draft
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
42 changes: 42 additions & 0 deletions demo/scripts/controlsV2/demoButtons/setCustomListStyleButton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { setCustomListStyle } from 'roosterjs-content-model-api';
import { showInputDialog } from 'roosterjs-react';
import type { RibbonButton } from 'roosterjs-react';

/**
* @internal
* "Set custom list style" button on the paragraph ribbon
*/
export const setCustomListStyleButton: RibbonButton<
'ribbonButtonCustomListStyle' | 'customListStyleDialogMarker'
> = {
key: 'ribbonButtonCustomListStyle',
unlocalizedText: 'Set custom list style',
iconName: 'BulletedList',
isDisabled: formatState => !formatState.isBullet,
onClick: (editor, _, strings, uiUtilities) => {
showInputDialog(
uiUtilities,
'ribbonButtonCustomListStyle',
'Set custom list style',
{
marker: {
labelKey: 'customListStyleDialogMarker',
unlocalizedLabel: 'Marker (include any trailing spaces)',
initValue: '\u2605 ',
autoFocus: true,
},
},
strings
).then(result => {
editor.focus();

if (result) {
if (!result.marker || result.marker.indexOf('\0') >= 0) {
alert('Enter non-empty marker text without null characters.');
} else {
setCustomListStyle(editor, result.marker);
}
}
});
},
};
3 changes: 3 additions & 0 deletions demo/scripts/controlsV2/tabs/ribbonButtons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { listStartNumberButton } from '../demoButtons/listStartNumberButton';
import { pasteAsMarkdownButton } from '../demoButtons/pasteAsMarkdownButton';
import { pasteButton } from '../demoButtons/pasteButton';
import { setBulletedListStyleButton } from '../demoButtons/setBulletedListStyleButton';
import { setCustomListStyleButton } from '../demoButtons/setCustomListStyleButton';
import { setListLevelButton } from '../demoButtons/setListLevelButton';
import { setNumberedListStyleButton } from '../demoButtons/setNumberedListStyleButton';
import { setTableCellShadeButton } from '../demoButtons/setTableCellShadeButton';
Expand Down Expand Up @@ -150,6 +151,7 @@ const paragraphButtons: RibbonButton<any>[] = [
rtlButton,
clearFormatButton,
setBulletedListStyleButton,
setCustomListStyleButton,
setNumberedListStyleButton,
listStartNumberButton,
setListLevelButton,
Expand Down Expand Up @@ -194,6 +196,7 @@ const allButtons: RibbonButton<any>[] = [
rtlButton,
clearFormatButton,
setBulletedListStyleButton,
setCustomListStyleButton,
setNumberedListStyleButton,
listStartNumberButton,
setListLevelButton,
Expand Down
2 changes: 2 additions & 0 deletions packages/roosterjs-content-model-api/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export { changeCapitalization } from './publicApi/segment/changeCapitalization';
export { splitTextSegment } from './publicApi/segment/splitTextSegment';
export { insertImage } from './publicApi/image/insertImage';
export { setListStyle } from './publicApi/list/setListStyle';
export { setCustomListStyle } from './publicApi/list/setCustomListStyle';
export { setListStartNumber } from './publicApi/list/setListStartNumber';
export { setListLevel } from './publicApi/list/setListLevel';
export { setIndentation } from './publicApi/block/setIndentation';
Expand Down Expand Up @@ -60,6 +61,7 @@ export { formatInsertPointWithContentModel } from './publicApi/utils/formatInser

export { setListType } from './modelApi/list/setListType';
export { setModelListStyle } from './modelApi/list/setModelListStyle';
export { setModelCustomListStyle } from './modelApi/list/setModelCustomListStyle';
export { setModelListStartNumber } from './modelApi/list/setModelListStartNumber';
export { findListItemsInSameThread } from './modelApi/list/findListItemsInSameThread';
export { setModelIndentation } from './modelApi/block/setModelIndentation';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { findListItemsInSameThread } from './findListItemsInSameThread';
import {
getFirstSelectedListItem,
mutateBlock,
updateListMetadata,
} from 'roosterjs-content-model-dom';
import type { ReadonlyContentModelDocument } from 'roosterjs-content-model-types';

/**
* Set a literal marker for the current bulleted list thread at the current nesting level.
* @param model The model document
* @param marker Literal marker text, including any desired spacing.
* @returns Whether a bulleted list was found
*/
export function setModelCustomListStyle(model: ReadonlyContentModelDocument, marker: string) {
const listItem = getFirstSelectedListItem(model);
const levelIndex = listItem ? listItem.levels.length - 1 : -1;

if (!listItem || listItem.levels[levelIndex]?.listType != 'UL') {
return false;
}

const listStyleType =
'"' +
marker.replace(/["\\\n\r\f]/g, character => {
return '\\' + character.charCodeAt(0).toString(16) + ' ';
}) +
'"';

findListItemsInSameThread(model, listItem).forEach(item => {
const mutableItem = mutateBlock(item);
const level = mutableItem.levels[levelIndex];

updateListMetadata(level, metadata => {
const result = { ...metadata };

delete result.unorderedStyleType;
delete result.applyListStyleFromLevel;

return result;
});
level.format.listStyleType = listStyleType;

// A built-in string marker or imported LI style would otherwise override the list.
if (mutableItem.levels.length == levelIndex + 1) {
delete mutableItem.format.listStyleType;
}
});

return true;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { setModelCustomListStyle } from '../../modelApi/list/setModelCustomListStyle';
import type { IEditor } from 'roosterjs-content-model-types';

/**
* Set a literal marker for the current bulleted list thread at the current nesting level.
* Numbered lists and paragraphs outside lists are unchanged. Nested markers are preserved.
* Use setListStyle to replace the custom marker with a built-in list style.
* @param editor The editor to operate on
* @param marker Literal text, including emoji and any desired trailing spaces.
* The caller must provide non-empty text without null characters.
* This is not a CSS value; quotes and backslashes are escaped automatically.
*/
export function setCustomListStyle(editor: IEditor, marker: string) {
editor.focus();

editor.formatContentModel(model => setModelCustomListStyle(model, marker), {
apiName: 'setCustomListStyle',
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import { ContentModelListItem } from 'roosterjs-content-model-types';
import { setModelCustomListStyle } from '../../../lib/modelApi/list/setModelCustomListStyle';
import {
BulletListType,
createContentModelDocument,
createListItem,
createListLevel,
createParagraph,
createSelectionMarker,
createText,
getListMetadata,
updateListMetadata,
} from 'roosterjs-content-model-dom';

describe('setModelCustomListStyle', () => {
function createItem(listType: 'UL' | 'OL' = 'UL', selected = false): ContentModelListItem {
const item = createListItem([createListLevel(listType)]);
const paragraph = createParagraph();

paragraph.segments.push(createText('item'));
if (selected) {
paragraph.segments.push(createSelectionMarker());
}
item.blocks.push(paragraph);

return item;
}

it('updates the whole thread, clears overriding styles and invalidates caches', () => {
const model = createContentModelDocument();
const first = createItem();
const selected = createItem('UL', true);
const last = createItem();
const selection = selected.blocks[0];

model.blocks.push(first, selected, last);
model.blocks.forEach(item => {
if (item.blockType == 'BlockGroup' && item.blockGroupType == 'ListItem') {
item.cachedElement = document.createElement('li');
item.levels[0].cachedElement = document.createElement('ul');
item.format.listStyleType = '"old"';
updateListMetadata(item.levels[0], () => ({
unorderedStyleType: BulletListType.Dash,
orderedStyleType: 1,
applyListStyleFromLevel: true,
}));
}
});

expect(setModelCustomListStyle(model, '\u2605 ')).toBe(true);

[first, selected, last].forEach(item => {
expect(item.levels[0].format.listStyleType).toBe('"\u2605 "');
expect(getListMetadata(item.levels[0])).toEqual({ orderedStyleType: 1 });
expect(item.format.listStyleType).toBeUndefined();
expect(item.cachedElement).toBeUndefined();
expect(item.levels[0].cachedElement).toBeUndefined();
});
expect(selected.blocks[0]).toBe(selection);
});

it('updates ancestor levels without changing nested markers or separated lists', () => {
const model = createContentModelDocument();
const selected = createItem('UL', true);
const nested = createItem();
const last = createItem();
const separate = createItem();

nested.levels.push(createListLevel('OL', { listStyleType: 'lower-alpha' }));
nested.format.listStyleType = '"nested"';
model.blocks.push(selected, nested, last, createParagraph(), separate);

expect(setModelCustomListStyle(model, '-> ')).toBe(true);

[selected, nested, last].forEach(item => {
expect(item.levels[0].format.listStyleType).toBe('"-> "');
});
expect(nested.levels[1].format.listStyleType).toBe('lower-alpha');
expect(nested.format.listStyleType).toBe('"nested"');
expect(separate.levels[0].format.listStyleType).toBeUndefined();
});

it('changes only the current nested thread', () => {
const model = createContentModelDocument();
const parent = createItem();
const nested = createItem('UL', true);
const sibling = createItem();
const lastParent = createItem();

nested.levels.push(createListLevel('UL'));
sibling.levels.push(createListLevel('UL'));
model.blocks.push(parent, nested, sibling, lastParent);

setModelCustomListStyle(model, 'x');

expect(nested.levels[1].format.listStyleType).toBe('"x"');
expect(sibling.levels[1].format.listStyleType).toBe('"x"');
[parent, nested, sibling, lastParent].forEach(item => {
expect(item.levels[0].format.listStyleType).toBeUndefined();
});
});

it('leaves ordered lists unchanged', () => {
const model = createContentModelDocument();
const item = createItem('OL', true);

model.blocks.push(item);

expect(setModelCustomListStyle(model, '*')).toBe(false);
expect(item.levels[0].format).toEqual({});
expect(item.levels[0].dataset).toEqual({});
});

it('does nothing outside a list', () => {
const model = createContentModelDocument();
const paragraph = createParagraph();

paragraph.segments.push(createSelectionMarker());
model.blocks.push(paragraph);

expect(setModelCustomListStyle(model, '*')).toBe(false);
expect(model.blocks).toEqual([paragraph]);
});

[
{ marker: '\u{1f680} ', expected: '"\u{1f680} "' },
{ marker: '"\\', expected: '"\\22 \\5c "' },
{ marker: '\n\r\f', expected: '"\\a \\d \\c "' },
{ marker: 'none; color: red', expected: '"none; color: red"' },
].forEach(({ marker, expected }) => {
it('escapes literal marker ' + JSON.stringify(marker), () => {
const model = createContentModelDocument();
const item = createItem('UL', true);

model.blocks.push(item);
setModelCustomListStyle(model, marker);

expect(item.levels[0].format.listStyleType).toBe(expected);
});
});
});
Loading
Loading