Skip to content

Commit d6321b0

Browse files
committed
feat(json-crdt-peritext-ui): 🎸 improve color formatting definition
1 parent 7a03736 commit d6321b0

File tree

6 files changed

+63
-9
lines changed

6 files changed

+63
-9
lines changed

src/json-crdt-extensions/peritext/registry/SliceRegistry.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,24 @@ export class SliceRegistry implements Printable {
7272
}),
7373
);
7474

75+
const colSchema = s.obj(
76+
{},
77+
{
78+
color: s.str<string>(''),
79+
},
80+
);
81+
registry.add(
82+
new SliceBehavior(SliceStacking.Many, TAG.col, 'Color', colSchema, false, void 0, {
83+
col: (jsonml) => {
84+
const attr = jsonml[1] || {};
85+
const data: JsonNodeView<SchemaToJsonNode<typeof colSchema>> = {
86+
color: attr.color ?? '',
87+
};
88+
return [TAG.col, {data, inline: true}] as PeritextMlElement<TAG.col, any, true>;
89+
},
90+
}),
91+
);
92+
7593
// TODO: add more default annotations with "Many" stacking behavior
7694
// comment = SliceTypeCon.comment,
7795
// font = SliceTypeCon.font,

src/json-crdt-peritext-ui/plugins/toolbar/cursor/caret/CaretBottomOverlay/index.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ export const CaretBottomOverlay: React.FC<CaretBottomOverlayProps> = (props) =>
2424
const inline = fwd || bwd;
2525
const {toolbar} = useToolbarPlugin()!;
2626

27-
console.log(toolbar.lastEvent?.detail);
28-
2927
if (!inline) return;
3028
if (!isDirectCaretPlacement(toolbar.lastEvent)) return;
3129

src/json-crdt-peritext-ui/plugins/toolbar/formatting/tags/a/Edit.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export const Edit: React.FC<EditProps> = ({formatting, onSave}) => {
5656
/>
5757
)}
5858
/>
59-
) : (
59+
) : !data.href ? null : (
6060
<Button
6161
icon={<Iconista set={'lucide'} icon={'plus'} width={16} height={16} />}
6262
size={-1}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as React from 'react';
2+
import {Iconista} from 'nice-ui/lib/icons/Iconista';
3+
import type {ToolbarSliceBehaviorData} from '../../../types';
4+
5+
export const behavior = {
6+
menu: {
7+
name: 'Color',
8+
icon: () => <Iconista width={16} height={16} set="lucide" icon="paintbrush" />,
9+
},
10+
validate: (formatting) => {
11+
const obj = formatting.conf()?.view() as {color: string};
12+
if (!obj || typeof obj !== 'object') return [{code: 'INVALID_CONFIG'}];
13+
const color = obj.color || '';
14+
if (typeof color !== 'string') return [{code: 'INVALID_COLOR'}];
15+
if (!/^#[0-9A-Fa-f]{6}$/.test(color)) return [{code: 'INVALID_COLOR'}];
16+
if (color.length < 4) return 'empty';
17+
return 'good';
18+
},
19+
previewText: (formatting) => {
20+
const data = formatting.conf()?.view() as {color: string};
21+
if (!data || typeof data !== 'object') return '';
22+
return data.color || '';
23+
},
24+
} satisfies ToolbarSliceBehaviorData;
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
import {behavior as a} from './a/behavior';
2+
import {behavior as col} from './col/behavior';
23

3-
export {a};
4+
export {a, col};

src/json-crdt-peritext-ui/plugins/toolbar/state/ToolbarState.tsx

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,20 @@ export class ToolbarState implements UiLifeCycles {
7373
// }
7474

7575
public startSliceConfig(tag: SliceTypeCon | string | number, menu?: MenuItem): NewFormatting | undefined {
76+
console.log(1);
7677
const editor = this.txt.editor;
7778
const behavior = editor.getRegistry().get(tag);
79+
console.log(2);
7880
const range = editor.mainCursor()?.range();
7981
if (!range) return;
82+
console.log(3);
8083
const newSlice = this.newSlice;
8184
if (!behavior) {
85+
console.log(4);
8286
newSlice.next(void 0);
8387
return;
8488
}
89+
console.log(5);
8590
const formatting = new NewFormatting(behavior, range, this);
8691
newSlice.next(formatting);
8792
return formatting;
@@ -103,6 +108,8 @@ export class ToolbarState implements UiLifeCycles {
103108

104109
const registry = this.txt.editor.getRegistry();
105110
Object.assign(registry.get(SliceTypeCon.a)?.data() || {}, behavior.a);
111+
// registry.add({});
112+
Object.assign(registry.get(SliceTypeCon.col)?.data() || {}, behavior.col);
106113

107114
const changeUnsubscribe = et.subscribe('change', (ev) => {
108115
const lastEvent = ev.detail.ev;
@@ -332,11 +339,7 @@ export class ToolbarState implements UiLifeCycles {
332339
name: 'Artistic',
333340
expand: 8,
334341
children: [
335-
{
336-
name: 'Color',
337-
icon: () => <Iconista width={16} height={16} set="lucide" icon="paintbrush" />,
338-
onSelect: () => {},
339-
},
342+
this.colorMenuItem(),
340343
{
341344
name: 'Background',
342345
icon: () => <Iconista width={16} height={16} set="lucide" icon="paint-bucket" />,
@@ -353,6 +356,16 @@ export class ToolbarState implements UiLifeCycles {
353356
};
354357
};
355358

359+
public readonly colorMenuItem = (): MenuItem => {
360+
const colorItem: MenuItem = {
361+
...behavior.col.menu,
362+
onSelect: () => {
363+
this.startSliceConfig(CommonSliceType.col, colorItem);
364+
},
365+
};
366+
return colorItem;
367+
};
368+
356369
public readonly linkMenuItem = (): MenuItem => {
357370
const linkAction: MenuItem = {
358371
...behavior.a.menu,

0 commit comments

Comments
 (0)