@use-kona/editor 0.1.30 → 0.1.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugins/CollapsibleBlocksPlugin/CollapsibleBlocksPlugin.js +5 -3
- package/dist/plugins/DnDPlugin/DnDPlugin.js +4 -2
- package/dist/src/plugins/CollapsibleBlocksPlugin/CollapsibleBlocksPlugin.d.ts +1 -0
- package/package.json +1 -1
- package/src/plugins/CollapsibleBlocksPlugin/CollapsibleBlocksPlugin.tsx +5 -6
- package/src/plugins/DnDPlugin/DnDPlugin.spec.ts +62 -0
- package/src/plugins/DnDPlugin/DnDPlugin.tsx +7 -3
|
@@ -17,7 +17,7 @@ class CollapsibleBlocksPlugin {
|
|
|
17
17
|
const editor = useSlate();
|
|
18
18
|
const path = ReactEditor.findPath(editor, props.element);
|
|
19
19
|
if (1 !== path.length) return props.children;
|
|
20
|
-
const isHidden =
|
|
20
|
+
const isHidden = isHiddenByCollapsedSection(editor, path);
|
|
21
21
|
if (isHidden) return /*#__PURE__*/ jsx("div", {
|
|
22
22
|
className: styles_module.hidden,
|
|
23
23
|
"data-collapsible-block-hidden": true,
|
|
@@ -94,7 +94,9 @@ const getCollapsibleSectionPaths = (editor, path)=>{
|
|
|
94
94
|
path[0] + index
|
|
95
95
|
]);
|
|
96
96
|
};
|
|
97
|
-
const
|
|
97
|
+
const isHiddenByCollapsedSection = (editor, path)=>{
|
|
98
|
+
const types = sectionTypesByEditor.get(editor);
|
|
99
|
+
if (!types) return false;
|
|
98
100
|
for(let index = 0; index < path[0]; index++){
|
|
99
101
|
const node = editor.children[index];
|
|
100
102
|
if (Element.isElement(node) && true === node.collapsed && types.includes(node.type) && path[0] < getSectionEnd(editor, index, types)) return true;
|
|
@@ -118,4 +120,4 @@ const getSectionEnd = (editor, boundaryIndex, types)=>{
|
|
|
118
120
|
}
|
|
119
121
|
return editor.children.length;
|
|
120
122
|
};
|
|
121
|
-
export { CollapsibleBlocksPlugin, getCollapsibleSectionPaths };
|
|
123
|
+
export { CollapsibleBlocksPlugin, getCollapsibleSectionPaths, isHiddenByCollapsedSection };
|
|
@@ -6,7 +6,7 @@ import { NativeTypes } from "react-dnd-html5-backend";
|
|
|
6
6
|
import { Editor, Element, Path, Transforms } from "slate";
|
|
7
7
|
import { ReactEditor, useReadOnly, useSlate } from "slate-react";
|
|
8
8
|
import { useEditorContext } from "../../provider.js";
|
|
9
|
-
import { getCollapsibleSectionPaths } from "../CollapsibleBlocksPlugin/CollapsibleBlocksPlugin.js";
|
|
9
|
+
import { getCollapsibleSectionPaths, isHiddenByCollapsedSection } from "../CollapsibleBlocksPlugin/CollapsibleBlocksPlugin.js";
|
|
10
10
|
import { getNodesByNodeIds } from "./utils.js";
|
|
11
11
|
class DnDPlugin {
|
|
12
12
|
options;
|
|
@@ -34,11 +34,12 @@ class DnDPlugin {
|
|
|
34
34
|
const dropTargetRef = useRef(null);
|
|
35
35
|
const customType = options.customTypes?.[props.element.type];
|
|
36
36
|
const currentElement = props.element;
|
|
37
|
+
const path = ReactEditor.findPath(editor, props.element);
|
|
38
|
+
const isHidden = isHiddenByCollapsedSection(editor, path);
|
|
37
39
|
const [, drag, preview] = useDrag({
|
|
38
40
|
type: customType?.type || DnDPlugin.DND_BLOCK_ELEMENT,
|
|
39
41
|
item: ()=>{
|
|
40
42
|
const selected = $selectedNodes;
|
|
41
|
-
const path = ReactEditor.findPath(editor, props.element);
|
|
42
43
|
const sectionPaths = getCollapsibleSectionPaths(editor, path);
|
|
43
44
|
const nodes = sectionPaths ? getNodesAtPaths(editor, sectionPaths) : selected.length > 0 && selected.find((node)=>node.nodeId === currentElement.nodeId) ? getNodesByNodeIds(editor, selected.map((node)=>node.nodeId)) : [
|
|
44
45
|
structuredClone(props.element)
|
|
@@ -142,6 +143,7 @@ class DnDPlugin {
|
|
|
142
143
|
if (this.options.ignoreNodes?.includes(props.element.type)) return props.children;
|
|
143
144
|
if (editor.isInline(props.element)) return props.children;
|
|
144
145
|
return /*#__PURE__*/ jsx("div", {
|
|
146
|
+
hidden: isHidden,
|
|
145
147
|
children: options.renderBlock?.({
|
|
146
148
|
props,
|
|
147
149
|
dragRef: drag,
|
|
@@ -11,3 +11,4 @@ export declare class CollapsibleBlocksPlugin implements IPlugin {
|
|
|
11
11
|
renderBlock: (props: RenderElementProps) => any;
|
|
12
12
|
}
|
|
13
13
|
export declare const getCollapsibleSectionPaths: (editor: Editor, path: Path) => number[][] | null;
|
|
14
|
+
export declare const isHiddenByCollapsedSection: (editor: Editor, path: Path) => boolean;
|
package/package.json
CHANGED
|
@@ -29,7 +29,7 @@ export class CollapsibleBlocksPlugin implements IPlugin {
|
|
|
29
29
|
return props.children;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
const isHidden =
|
|
32
|
+
const isHidden = isHiddenByCollapsedSection(editor, path);
|
|
33
33
|
|
|
34
34
|
if (isHidden) {
|
|
35
35
|
return (
|
|
@@ -129,11 +129,10 @@ export const getCollapsibleSectionPaths = (editor: Editor, path: Path) => {
|
|
|
129
129
|
return Array.from({ length: end - path[0] }, (_, index) => [path[0] + index]);
|
|
130
130
|
};
|
|
131
131
|
|
|
132
|
-
const
|
|
133
|
-
editor
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
) => {
|
|
132
|
+
export const isHiddenByCollapsedSection = (editor: Editor, path: Path) => {
|
|
133
|
+
const types = sectionTypesByEditor.get(editor);
|
|
134
|
+
if (!types) return false;
|
|
135
|
+
|
|
137
136
|
for (let index = 0; index < path[0]; index++) {
|
|
138
137
|
const node = editor.children[index] as CollapsibleElement;
|
|
139
138
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// @vitest-environment jsdom
|
|
2
2
|
|
|
3
|
+
import '@testing-library/jest-dom/vitest';
|
|
3
4
|
import { act, render, waitFor } from '@testing-library/react';
|
|
4
5
|
import { createDragDropManager, type DragDropManager } from 'dnd-core';
|
|
5
6
|
import React from 'react';
|
|
@@ -343,4 +344,65 @@ describe('DnDPlugin', () => {
|
|
|
343
344
|
]);
|
|
344
345
|
await waitFor(() => expect(onChange).toHaveBeenCalled());
|
|
345
346
|
});
|
|
347
|
+
|
|
348
|
+
it('hides custom drag wrappers for collapsed section content', async () => {
|
|
349
|
+
const manager = createDndManager();
|
|
350
|
+
const dndPlugin = new DnDPlugin({
|
|
351
|
+
onDropFiles: () => {},
|
|
352
|
+
renderBlock: ({ dragRef, dropRef, previewRef, props }) =>
|
|
353
|
+
React.createElement(
|
|
354
|
+
'div',
|
|
355
|
+
{
|
|
356
|
+
...props.attributes,
|
|
357
|
+
'data-dnd-block': (props.element as { nodeId: string }).nodeId,
|
|
358
|
+
ref: (element) => dropRef(previewRef(element)),
|
|
359
|
+
},
|
|
360
|
+
React.createElement(
|
|
361
|
+
'button',
|
|
362
|
+
{ contentEditable: false, ref: dragRef, type: 'button' },
|
|
363
|
+
'Drag',
|
|
364
|
+
),
|
|
365
|
+
props.children,
|
|
366
|
+
),
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
render(
|
|
370
|
+
React.createElement(
|
|
371
|
+
DndProvider,
|
|
372
|
+
{ manager },
|
|
373
|
+
React.createElement(KonaEditor, {
|
|
374
|
+
initialValue: [
|
|
375
|
+
{
|
|
376
|
+
type: HeadingsPlugin.HeadingLevel1,
|
|
377
|
+
nodeId: 'section',
|
|
378
|
+
collapsed: true,
|
|
379
|
+
children: [{ text: 'Section' }],
|
|
380
|
+
},
|
|
381
|
+
{
|
|
382
|
+
type: 'paragraph',
|
|
383
|
+
nodeId: 'section-text',
|
|
384
|
+
children: [{ text: 'Section text' }],
|
|
385
|
+
},
|
|
386
|
+
{
|
|
387
|
+
type: HeadingsPlugin.HeadingLevel1,
|
|
388
|
+
nodeId: 'next-section',
|
|
389
|
+
children: [{ text: 'Next section' }],
|
|
390
|
+
},
|
|
391
|
+
],
|
|
392
|
+
plugins: [
|
|
393
|
+
new HeadingsPlugin(),
|
|
394
|
+
new CollapsibleBlocksPlugin([HeadingsPlugin.HeadingLevel1]),
|
|
395
|
+
dndPlugin,
|
|
396
|
+
],
|
|
397
|
+
onChange: () => {},
|
|
398
|
+
}),
|
|
399
|
+
),
|
|
400
|
+
);
|
|
401
|
+
|
|
402
|
+
const hiddenDragBlock = document.querySelector(
|
|
403
|
+
'[data-dnd-block="section-text"]',
|
|
404
|
+
);
|
|
405
|
+
|
|
406
|
+
expect(hiddenDragBlock).not.toBeVisible();
|
|
407
|
+
});
|
|
346
408
|
});
|
|
@@ -12,7 +12,10 @@ import {
|
|
|
12
12
|
import type { CustomElement } from '../../../types';
|
|
13
13
|
import { useEditorContext } from '../../provider';
|
|
14
14
|
import type { IPlugin } from '../../types';
|
|
15
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
getCollapsibleSectionPaths,
|
|
17
|
+
isHiddenByCollapsedSection,
|
|
18
|
+
} from '../CollapsibleBlocksPlugin/CollapsibleBlocksPlugin';
|
|
16
19
|
import type { EditorDragItem, NodeWithId, Options } from './types';
|
|
17
20
|
import { getNodesByNodeIds } from './utils';
|
|
18
21
|
|
|
@@ -50,12 +53,13 @@ export class DnDPlugin implements IPlugin {
|
|
|
50
53
|
|
|
51
54
|
const customType = options.customTypes?.[props.element.type];
|
|
52
55
|
const currentElement = props.element as NodeWithId;
|
|
56
|
+
const path = ReactEditor.findPath(editor, props.element);
|
|
57
|
+
const isHidden = isHiddenByCollapsedSection(editor, path);
|
|
53
58
|
|
|
54
59
|
const [, drag, preview] = useDrag({
|
|
55
60
|
type: customType?.type || DnDPlugin.DND_BLOCK_ELEMENT,
|
|
56
61
|
item: () => {
|
|
57
62
|
const selected: Array<NodeWithId> = $selectedNodes;
|
|
58
|
-
const path = ReactEditor.findPath(editor, props.element);
|
|
59
63
|
const sectionPaths = getCollapsibleSectionPaths(editor, path);
|
|
60
64
|
|
|
61
65
|
/**
|
|
@@ -252,7 +256,7 @@ export class DnDPlugin implements IPlugin {
|
|
|
252
256
|
}
|
|
253
257
|
|
|
254
258
|
return (
|
|
255
|
-
<div>
|
|
259
|
+
<div hidden={isHidden}>
|
|
256
260
|
{options.renderBlock?.({
|
|
257
261
|
props,
|
|
258
262
|
dragRef: drag,
|