Skip to content

Commit

Permalink
feat: Support read only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hata6502 committed Feb 3, 2021
1 parent 4c8839b commit 5e45072
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 145 deletions.
168 changes: 89 additions & 79 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
<button id="button" type="button">Save</button>
<div id="output-data"></div>

<br />

read only mode
<div id="read-only-editorjs"></div>

<script src="https://cdn.jsdelivr.net/npm/@editorjs/editorjs@latest"></script>

<!-- For demo -->
Expand Down Expand Up @@ -66,95 +71,93 @@

const editorJSConfig = {};

const editorJS = new EditorJS({
data,
tools: {
layout: {
class: EditorJSLayout.LayoutBlockTool,
config: {
EditorJS,
editorJSConfig,
enableLayoutEditing: false,
enableLayoutSaving: true,
initialData: {
itemContent: {
1: {
blocks: [],
},
},
layout: {
type: "container",
id: "",
className: "",
style: "border: 1px solid #000000; ",
children: [
{
type: "item",
id: "",
className: "",
style:
"border: 1px solid #000000; display: inline-block; ",
itemContentId: "1",
},
],
const tools = {
layout: {
class: EditorJSLayout.LayoutBlockTool,
config: {
EditorJS,
editorJSConfig,
enableLayoutEditing: false,
enableLayoutSaving: true,
initialData: {
itemContent: {
1: {
blocks: [],
},
},
layout: {
type: "container",
id: "",
className: "",
style: "border: 1px solid #000000; ",
children: [
{
type: "item",
id: "",
className: "",
style: "border: 1px solid #000000; display: inline-block; ",
itemContentId: "1",
},
],
},
},
},
twoColumns: {
class: EditorJSLayout.LayoutBlockTool,
config: {
EditorJS,
editorJSConfig,
enableLayoutEditing: false,
enableLayoutSaving: false,
initialData: {
itemContent: {
1: {
blocks: [],
},
2: {
blocks: [],
},
},
twoColumns: {
class: EditorJSLayout.LayoutBlockTool,
config: {
EditorJS,
editorJSConfig,
enableLayoutEditing: false,
enableLayoutSaving: false,
initialData: {
itemContent: {
1: {
blocks: [],
},
layout: {
type: "container",
id: "",
className: "",
style:
"border: 1px solid #000000; display: flex; justify-content: space-around; padding: 16px; ",
children: [
{
type: "item",
id: "",
className: "",
style: "border: 1px solid #000000; padding: 8px; ",
itemContentId: "1",
},
{
type: "item",
id: "",
className: "",
style: "border: 1px solid #000000; padding: 8px; ",
itemContentId: "2",
},
],
2: {
blocks: [],
},
},
layout: {
type: "container",
id: "",
className: "",
style:
"border: 1px solid #000000; display: flex; justify-content: space-around; padding: 16px; ",
children: [
{
type: "item",
id: "",
className: "",
style: "border: 1px solid #000000; padding: 8px; ",
itemContentId: "1",
},
{
type: "item",
id: "",
className: "",
style: "border: 1px solid #000000; padding: 8px; ",
itemContentId: "2",
},
],
},
},
shortcut: "CMD+2",
toolbox: {
icon: `
<svg xmlns='http://www.w3.org/2000/svg' width="16" height="16" viewBox='0 0 512 512'>
<rect x='128' y='128' width='336' height='336' rx='57' ry='57' fill='none' stroke='currentColor' stroke-linejoin='round' stroke-width='32'/>
<path d='M383.5 128l.5-24a56.16 56.16 0 00-56-56H112a64.19 64.19 0 00-64 64v216a56.16 56.16 0 0056 56h24' fill='none' stroke='currentColor' stroke-linecap='round' stroke-linejoin='round' stroke-width='32'/>
</svg>
`,
title: "2 columns",
},
},
shortcut: "CMD+2",
toolbox: {
icon: `
<svg xmlns='http://www.w3.org/2000/svg' width="16" height="16" viewBox='0 0 512 512'>
<rect x='128' y='128' width='336' height='336' rx='57' ry='57' fill='none' stroke='currentColor' stroke-linejoin='round' stroke-width='32'/>
<path d='M383.5 128l.5-24a56.16 56.16 0 00-56-56H112a64.19 64.19 0 00-64 64v216a56.16 56.16 0 0056 56h24' fill='none' stroke='currentColor' stroke-linecap='round' stroke-linejoin='round' stroke-width='32'/>
</svg>
`,
title: "2 columns",
},
},
});
};

const editorJS = new EditorJS({ data, tools });

document.querySelector("#button").addEventListener("click", async () => {
const outputData = await editorJS.save();
Expand All @@ -163,6 +166,13 @@
outputData
);
});

new EditorJS({
holder: "read-only-editorjs",
data,
readOnly: true,
tools,
});
</script>
</body>
</html>
8 changes: 8 additions & 0 deletions src/LayoutBlockTool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ interface LayoutBlockToolDispatchData {
}

class LayoutBlockTool implements BlockTool {
static get isReadOnlySupported() {
return true;
}

static get shortcut() {
return "CMD+L";
}
Expand All @@ -68,6 +72,7 @@ class LayoutBlockTool implements BlockTool {
}

#config!: LayoutBlockToolConfig;
#readOnly: boolean;
#wrapper: HTMLDivElement;

#itemContent: LayoutBlockItemContentData;
Expand All @@ -76,7 +81,9 @@ class LayoutBlockTool implements BlockTool {
constructor({
config,
data,
readOnly,
}: BlockToolConstructorOptions<LayoutBlockToolData, LayoutBlockToolConfig>) {
this.#readOnly = readOnly;
this.#wrapper = document.createElement("div");

this.#itemContent = {};
Expand Down Expand Up @@ -152,6 +159,7 @@ class LayoutBlockTool implements BlockTool {
dispatchData: this.#dispatchData,
editorJSConfig: this.#config.editorJSConfig,
itemContentData: this.#itemContent,
readOnly: this.#readOnly,
})
);
}
Expand Down
37 changes: 21 additions & 16 deletions src/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,23 @@ interface ValidatedLayoutBlockContainerData extends LayoutBlockContainerData {
)[];
}

const renderContainer = ({
EditorJS,
data,
dispatchData,
editorJSConfig,
itemContentData,
}: {
interface RenderContext {
EditorJS: LayoutBlockToolConfig["EditorJS"];
data: LayoutBlockContainerData;
dispatchData: LayoutBlockToolDispatchData;
editorJSConfig: LayoutBlockToolConfig["editorJSConfig"];
readOnly: boolean;
}

interface RenderContainerProps extends RenderContext {
data: LayoutBlockContainerData;
itemContentData: LayoutBlockItemContentData;
}) => {
}

const renderContainer = ({
data,
itemContentData,
...context
}: RenderContainerProps) => {
const wrapper = document.createElement("div");

wrapper.id = data.id;
Expand All @@ -47,10 +51,8 @@ const renderContainer = ({
switch (child.type) {
case "container": {
childElement = renderContainer({
EditorJS,
...context,
data: child,
dispatchData,
editorJSConfig,
itemContentData,
});

Expand All @@ -59,10 +61,8 @@ const renderContainer = ({

case "item": {
childElement = renderItem({
EditorJS,
...context,
data: child,
dispatchData,
editorJSConfig,
itemContentData,
});

Expand All @@ -83,4 +83,9 @@ const renderContainer = ({
};

export { renderContainer };
export type { LayoutBlockContainerData, ValidatedLayoutBlockContainerData };
export type {
LayoutBlockContainerData,
RenderContainerProps,
RenderContext,
ValidatedLayoutBlockContainerData,
};
30 changes: 11 additions & 19 deletions src/item.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import type {
LayoutBlockToolConfig,
LayoutBlockToolDispatchData,
} from "./LayoutBlockTool";
import type { RenderContext } from "./container";
import { renderItemContent } from "./itemContent";
import type { LayoutBlockItemContentData } from "./itemContent";

Expand All @@ -15,19 +12,12 @@ interface LayoutBlockItemData {

interface ValidatedLayoutBlockItemData extends LayoutBlockItemData {}

const renderItem = ({
EditorJS,
data,
dispatchData,
editorJSConfig,
itemContentData,
}: {
EditorJS: LayoutBlockToolConfig["EditorJS"];
interface RenderItemProps extends RenderContext {
data: LayoutBlockItemData;
dispatchData: LayoutBlockToolDispatchData;
editorJSConfig: LayoutBlockToolConfig["editorJSConfig"];
itemContentData: LayoutBlockItemContentData;
}) => {
}

const renderItem = ({ data, itemContentData, ...context }: RenderItemProps) => {
const wrapper = document.createElement("div");

wrapper.id = data.id;
Expand All @@ -38,10 +28,8 @@ const renderItem = ({

wrapper.append(
renderItemContent({
EditorJS,
...context,
data: editorJSData,
dispatchData,
editorJSConfig,
itemContentId: data.itemContentId,
})
);
Expand All @@ -50,4 +38,8 @@ const renderItem = ({
};

export { renderItem };
export type { LayoutBlockItemData, ValidatedLayoutBlockItemData };
export type {
LayoutBlockItemData,
RenderItemProps,
ValidatedLayoutBlockItemData,
};
Loading

0 comments on commit 5e45072

Please sign in to comment.