Skip to content

Commit 7a3359e

Browse files
committed
Allow access to the seven block id from an editor handler
1 parent 9fcde5b commit 7a3359e

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

packages/cmsui/routes/edit.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@ export default function Edit() {
100100
]);
101101
const initialValue = blocksToPlateNew(content);
102102

103+
const editorCallbacks = {
104+
openSideBar: (id: string) => {
105+
console.log('editorCallbacks.openSideBar called with id:', id);
106+
},
107+
};
108+
103109
return (
104110
<InitAtoms atomValues={[[formAtom, content]]}>
105111
<main className="mx-4 mt-8 flex h-screen flex-1 flex-col">
@@ -209,6 +215,7 @@ export default function Edit() {
209215
field.handleChange(newValue);
210216
}}
211217
emblaApi={emblaApi2}
218+
editorCallbacks={editorCallbacks}
212219
/>
213220
</div>
214221
<div className="mt-4 w-[900px]">
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export const getSevenBlockIdFromHandler = (api: any): string =>
2+
// Find the first node above the current node which has an id which is an uuid.
3+
// Unfortunately, the sub-nodes in the tree can also have ids, which are not
4+
// Seven ids, so we need to filter them out.k
5+
api.above({
6+
match: (node: any) =>
7+
node.id?.match(
8+
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/,
9+
),
10+
})[0]?.id;

packages/editor/components/editor/plate-editor.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,14 @@ import { useSetAtom } from 'jotai';
1313
export function PlateEditor(props: {
1414
value?: Value;
1515
onChange?: (value: Value) => void;
16+
editorCallbacks?: Record<string, any>;
1617
}) {
17-
const editor = useCreateEditor({ value: props.value });
18+
const editor = useCreateEditor({
19+
value: props.value,
20+
});
1821
const setEditorState = useSetAtom(editorAtom);
1922
editor.emblaApi = props.emblaApi;
23+
editor.editorCallbacks = props.editorCallbacks;
2024

2125
return (
2226
<>

packages/editor/components/editor/use-create-editor.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,22 @@ import { TableRowElement } from '@/components/plate-ui/table-row-element';
9999
import { TocElement } from '@/components/plate-ui/toc-element';
100100
import { ToggleElement } from '@/components/plate-ui/toggle-element';
101101
import UnknownElement from '@/components/plate-ui/unknown-element';
102+
import { getSevenBlockIdFromHandler } from '@/components/editor/get-seven-block-id-from-handler';
102103

103104
const TitlePlugin = createPlatePlugin({
104105
key: 'title',
105106
node: {
106107
isElement: true,
107108
type: 'title',
108109
},
109-
// handlers: {
110-
// onKeyDown: ({ editor }) => {
111-
// // Prevent default behavior for title
112-
// console.log('TitlePlugin: onKeyDown');
113-
// },
114-
// },
110+
handlers: {
111+
onClick: ({ editor, api }) => {
112+
editor.editorCallbacks?.openSideBar?.(getSevenBlockIdFromHandler(api));
113+
},
114+
onKeyDown: ({ editor, api }) => {
115+
editor.editorCallbacks?.openSideBar?.(getSevenBlockIdFromHandler(api));
116+
},
117+
},
115118
});
116119

117120
const UnknownElementPlugin = createPlatePlugin({

0 commit comments

Comments
 (0)