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

Feat/checked marker #3073

Merged
merged 9 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 14 additions & 3 deletions apps/www/src/lib/plate/demo/values/indentListValue.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,24 @@ jsx;
export const indentListValue: any = (
<fragment>
<hh2>Indent List</hh2>
<hp indent={1} listStyleType="todo" checked={true}>
Decimal 112
</hp>

<hp>
Create indented lists with multiple levels of indentation and customize
the list style type for each level.
</hp>
<hp indent={1} listStyleType="todo" checked={true}>
Todo 1
</hp>

<hp indent={1} listStyleType="fire">
Icon 1
</hp>
<hp indent={2} listStyleType="fire">
Icon 2
</hp>
<hp indent={3} listStyleType="todo" checked={false}>
Todo 2
</hp>
<hp indent={1} listStyleType="upper-roman">
Roman 1
</hp>
Expand Down
24 changes: 21 additions & 3 deletions apps/www/src/registry/default/example/playground-demo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ import { Editor } from '@/registry/default/plate-ui/editor';
import { FixedToolbar } from '@/registry/default/plate-ui/fixed-toolbar';
import { FloatingToolbar } from '@/registry/default/plate-ui/floating-toolbar';
import {
CheckedMarker,
FireLiComponent,
FireMarker,
} from '@/registry/default/plate-ui/indent-fire-marker-component';
import {
IndentTodoLiComponent,
TodoMarker,
} from '@/registry/default/plate-ui/indent-todo-marker-component';
import { MentionCombobox } from '@/registry/default/plate-ui/mention-combobox';
Expand Down Expand Up @@ -237,8 +241,22 @@ export const usePlaygroundPlugins = ({
},
enabled: id === 'indentlist' || !!enabled.listStyleType,
options: {
markerComponent: TodoMarker,
markerCheckedStyle: CheckedMarker,
listStyleTypes: {
['upper-roman']: {
type: 'upper-roman',
isNumbered: true,
},
todo: {
type: 'todo',
markerComponent: TodoMarker,
liComponent: IndentTodoLiComponent,
},
fire: {
type: 'fire',
markerComponent: FireMarker,
liComponent: FireLiComponent,
},
},
},
}),
createLineHeightPlugin({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { cn } from '@udecode/cn';

export const FireMarker = (props: any) => {
const { element } = props;

return (
<div contentEditable={false}>
<span style={{ left: -26, top: -1, position: 'absolute' }}>
{element.indent % 2 === 0 ? '🔥' : '🚀'}
</span>
</div>
);
};

export const FireLiComponent = (props: any) => {
const { element, children } = props;
return (
<span
className={cn(element.checked && 'text-muted-foreground line-through')}
>
{children}
</span>
);
};
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
import { cn } from '@udecode/cn';
import {
IMarkerCheckedStyle,
IMarkerComponentProps,
} from '@udecode/plate-indent-list';
import { setNodes } from '@udecode/slate';
import { findNodePath } from '@udecode/slate-react';

import { Checkbox } from './checkbox';

export const TodoMarker = (props: IMarkerComponentProps) => {
const { onChange, checked } = props;
export const TodoMarker = (props: any) => {
const { editor, element } = props;

const onChange = (v: boolean) => {
const path = findNodePath(editor, element);
setNodes(editor, { checked: v }, { at: path });
};

return (
<Checkbox
style={{ left: -24, top: 4, position: 'absolute' }}
onCheckedChange={onChange}
checked={checked}
/>
<div contentEditable={false}>
<Checkbox
style={{ left: -24, top: 4, position: 'absolute' }}
checked={element.checked}
onCheckedChange={onChange}
/>
</div>
);
};

export const CheckedMarker = (props: IMarkerCheckedStyle) => {
const { checked, children } = props;
export const IndentTodoLiComponent = (props: any) => {
const { element, children } = props;
return (
<span className={cn(checked && 'text-muted-foreground line-through')}>
<span
className={cn(element.checked && 'text-muted-foreground line-through')}
>
{children}
</span>
);
Expand Down
13 changes: 10 additions & 3 deletions packages/indent-list/src/createIndentListPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { injectIndentListComponent } from './injectIndentListComponent';
import { onKeyDownIndentList } from './onKeyDownIndentList';
import { GetSiblingIndentListOptions } from './queries/getSiblingIndentList';
import {
IMarkerCheckedStyle,
ILiComponentProps,
IMarkerComponentProps,
ListStyleType,
} from './types';
Expand All @@ -33,8 +33,15 @@ export interface IndentListPlugin {
*/
getListStyleType?: (element: HTMLElement) => ListStyleType;

markerComponent?: React.FC<IMarkerComponentProps>;
markerCheckedStyle?: React.FC<IMarkerCheckedStyle>;
listStyleTypes?: Record<
string,
{
type: string;
markerComponent?: React.FC<IMarkerComponentProps>;
liComponent?: React.FC<ILiComponentProps>;
isNumbered?: boolean;
}
>;
}

export const createIndentListPlugin = createPluginFactory<IndentListPlugin>({
Expand Down
101 changes: 25 additions & 76 deletions packages/indent-list/src/injectIndentListComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
import React from 'react';
import {
findNodePath,
getPluginOptions,
InjectComponentProps,
InjectComponentReturnType,
setNodes,
} from '@udecode/plate-common';
import { clsx } from 'clsx';

import {
IndentListPlugin,
KEY_LIST_CHECKED,
KEY_LIST_START,
KEY_LIST_STYLE_TYPE,
KEY_TODO_STYLE_TYPE,
} from './createIndentListPlugin';
import { ListStyleType } from './types';

export const injectIndentListComponent = (
props: InjectComponentProps
Expand All @@ -25,90 +20,44 @@ export const injectIndentListComponent = (
const listStyleType = element[KEY_LIST_STYLE_TYPE] as string;
const listStart = element[KEY_LIST_START] as number;

const isTodo =
element.hasOwnProperty(KEY_LIST_CHECKED) &&
listStyleType === KEY_TODO_STYLE_TYPE;

if (listStyleType && !isTodo) {
if (listStyleType) {
let className = clsx(`slate-${KEY_LIST_STYLE_TYPE}-${listStyleType}`);
const style: React.CSSProperties = {
padding: 0,
margin: 0,
listStyleType,
position: 'relative',
};

if (
[ListStyleType.Disc, ListStyleType.Circle, ListStyleType.Square].includes(
listStyleType as ListStyleType
)
) {
className = clsx(className, 'slate-list-bullet');
return function Ul({ editor, children }) {
const { listStyleTypes = {} } = getPluginOptions<IndentListPlugin>(
editor,
KEY_LIST_STYLE_TYPE
);

return function Ul({ children }) {
return (
<ul style={style} className={className}>
<li>{children}</li>
</ul>
);
};
}
const targetList = listStyleTypes[listStyleType] ?? {};
const isNumbered = targetList ? targetList.isNumbered : false;

className = clsx(className, 'slate-list-number');
className = isNumbered
? clsx(className, 'slate-list-number')
: clsx(className, 'slate-list-bullet');

return function Ol({ children }) {
return (
<ol style={style} className={className} start={listStart}>
<li>{children}</li>
</ol>
);
};
}
const {
markerComponent = null,
// eslint-disable-next-line @typescript-eslint/no-shadow
liComponent = ({ children }: any) => <li>{children}</li>,
} = targetList;

if (isTodo) {
const className = clsx('slate-list-todo');
const checked = element[KEY_LIST_CHECKED] as boolean;
const style: React.CSSProperties = {
position: 'relative',
padding: 0,
margin: 0,
};
return function Ol({ children, editor }) {
const { markerComponent, markerCheckedStyle } =
getPluginOptions<IndentListPlugin>(editor, KEY_LIST_STYLE_TYPE);
const Wrap = isNumbered ? 'ol' : 'ul';

return (
<div className={`${className}`} style={style}>
<div contentEditable={false}>
{markerComponent ? (
markerComponent({
checked: checked,
onChange: (v: boolean) => {
const path = findNodePath(editor, element);
setNodes(editor, { checked: v }, { at: path });
},
})
) : (
<input
type="checkbox"
style={{ position: 'absolute', left: -19, top: 6 }}
checked={checked}
onChange={(v) => {
const path = findNodePath(editor, element);
setNodes(editor, { checked: v.target.checked }, { at: path });
}}
/>
)}
</div>

{markerCheckedStyle ? (
markerCheckedStyle({
checked: checked,
children: children,
})
) : (
<span>{children}</span>
)}
</div>
<Wrap style={style} className={className} start={listStart}>
{markerComponent && markerComponent({ editor, element })}
{liComponent({
children,
element,
})}
</Wrap>
);
};
}
Expand Down
11 changes: 7 additions & 4 deletions packages/indent-list/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { PlateEditor, TElement, Value } from '@udecode/plate-common';

export enum ListStyleType {
// The marker is traditional Armenian numbering
Armenian = 'armenian',
Expand Down Expand Up @@ -69,12 +71,13 @@ export enum ListStyleType {
Inherit = 'inherit',
}

export interface IMarkerCheckedStyle {
checked: boolean;
export interface ILiComponentProps {
felixfeng33 marked this conversation as resolved.
Show resolved Hide resolved
element: TElement;
children: any;
}

export interface IMarkerComponentProps {
felixfeng33 marked this conversation as resolved.
Show resolved Hide resolved
onChange: (checked: boolean) => void;
checked: boolean;
onChange?: (checked: boolean) => void;
element: TElement;
editor: PlateEditor<Value>;
}