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

Content Model: Set alignment API #1650

Merged
merged 9 commits into from
Mar 21, 2023

Conversation

juliaroldi
Copy link
Contributor

@juliaroldi juliaroldi commented Mar 17, 2023

Add setModelAlignment api that supports align lists and tables.
alignment

@juliaroldi juliaroldi marked this pull request as ready for review March 17, 2023 15:13
@@ -35,7 +35,7 @@ describe('listItemProcessor', () => {
});
});

it('LI with display:block', () => {
xit('LI with display:block', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we really need to skip this test?

/**
* The format object for a list item in Content Model
*/
export type ContentModelListItemFormat = DirectionFormat;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need to export this type in index.ts

@@ -51,7 +52,7 @@ export interface ContentModelFormatMap {
/**
* Format type for listItem
*/
listItem: ContentModelListItemLevelFormat;
listItem: ContentModelListItemFormat;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I think we need to add a new item here, but not change the existing one.

listItem handles item level format, which is also stored in ContentModelListItemLevelFormat. So the new format is different, need another handler set.

applyFormat(li, context.formatAppliers.segment, listItem.formatHolder.format, context);
applyFormat(li, context.formatAppliers.listItem, level, context);
applyFormat(li, context.formatAppliers.listLevel, level, context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

listLevel should be only applied to OL/UL, but not LI.

@@ -8,14 +8,19 @@ import { createSelectionMarker } from './createSelectionMarker';
*/
export function createListItem(
levels: ContentModelListItemLevelFormat[],
format?: ContentModelSegmentFormat
format?: ContentModelSegmentFormat,
li?: HTMLLIElement
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try avoid passing in DOM element into creator. You can pass in its style value directly.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this should be done by format handler, but don't handle format directly in creator

@@ -94,7 +94,7 @@ const defaultFormatKeysPerCategory: {
[key in keyof ContentModelFormatMap]: (keyof FormatHandlerTypeMap)[];
} = {
block: blockFormatHandlers,
listItem: ['listItemThread', 'listItemMetadata'],
listItem: ['listItemThread', 'listItemMetadata', 'direction'],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, let's add a new item for direction

@@ -23,12 +23,14 @@ export const listItemProcessor: ElementProcessor<HTMLLIElement> = (group, elemen
context
);

const listItem = createListItem(listFormat.levels, context.segmentFormat);
const listItem = createListItem(listFormat.levels, context.segmentFormat, element);
parseFormat(element, context.formatParsers.listItem, listItem.format, context);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, use a different format handler set.

listFormat.listParent!.blocks.push(listItem);

parseFormat(
element,
context.formatParsers.listItem,
context.formatParsers.listLevel,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

don't change this. listItem and listLevel are doing different job.

Comment on lines +69 to +71
} else if (block) {
const { format } = block;
format.textAlign = newAligment;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this part is the same with line 63. We can merge them

@@ -54,7 +54,7 @@ describe('listItemProcessor', () => {
blockType: 'BlockGroup',
blockGroupType: 'ListItem',
blocks: [],
levels: [{ listType: 'UL', displayForDummyItem: 'block' }],
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows the code has regression. displayForDummyItem needs to be 'block' here

@juliaroldi juliaroldi marked this pull request as draft March 17, 2023 18:37
@juliaroldi juliaroldi marked this pull request as ready for review March 17, 2023 21:56
@juliaroldi juliaroldi force-pushed the u/juliaroldi/set-alignment-content-model branch from b9ce59f to a6cc71c Compare March 17, 2023 21:57
@@ -8,14 +8,19 @@ import { createSelectionMarker } from './createSelectionMarker';
*/
export function createListItem(
levels: ContentModelListItemLevelFormat[],
format?: ContentModelSegmentFormat
format?: ContentModelSegmentFormat,
textAlign?: string
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I commented in another place, this should be done by format handler, but not through a parameter of creator.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the reason is, here you allow to pass a string, then later you convert the type to "start|end|center". What if people pass in some other string? We can't detect it and there is no build time error. So that will most likely cause bug. To solve it, people need to convert their string to the type we need. But that is already done in format handler, so it is a dup.

const listItem = createListItem(
listFormat.levels,
context.segmentFormat,
element.style.alignSelf
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here, leave it to format handler.

export const listItemElementFormatHandler: FormatHandler<ContentModelListItemFormat> = {
parse: (format, element) => {
const alignment = element.style.alignSelf;
if (alignment && ['end', 'start', 'center'].indexOf(alignment) > -1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all the logic here is already included in directionFormatHandler, right? Then we don't need this handler at all.

@juliaroldi juliaroldi force-pushed the u/juliaroldi/set-alignment-content-model branch from ac523ef to 56bb98d Compare March 20, 2023 21:49
@@ -48,7 +49,7 @@ export interface ContentModelFormatMap {
*/
tableCell: ContentModelTableCellFormat;

/**
/*
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It happened when I was solving conflicts. I'll fix.

@juliaroldi juliaroldi merged commit 527cc0a into master Mar 21, 2023
@juliaroldi juliaroldi deleted the u/juliaroldi/set-alignment-content-model branch March 21, 2023 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants