Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: remove custom inline image -> block image migration

Abdullah Atta7af159603805
diff --git a/packages/editor/src/extensions/image/__tests__/__snapshots__/image.test.ts.snap b/packages/editor/src/extensions/image/__tests__/__snapshots__/image.test.ts.snap
new file mode 100644
index 000000000..acd067595
--- /dev/null
+++ b/packages/editor/src/extensions/image/__tests__/__snapshots__/image.test.ts.snap
@@ -0,0 +1,171 @@
+// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
+
+exports[`migration > inline image in outline list 1`] = `
+{
+ "content": [
+ {
+ "content": [
+ {
+ "attrs": {
+ "collapsed": false,
+ },
+ "content": [
+ {
+ "content": [
+ {
+ "text": "item 1",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "outlineListItem",
+ },
+ {
+ "attrs": {
+ "collapsed": false,
+ },
+ "content": [
+ {
+ "content": [
+ {
+ "text": "hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ {
+ "attrs": {
+ "align": undefined,
+ "aspectRatio": 1,
+ "filename": undefined,
+ "hash": undefined,
+ "height": null,
+ "mime": undefined,
+ "progress": 0,
+ "size": undefined,
+ "src": "image.png",
+ "type": "image",
+ "width": null,
+ },
+ "type": "image",
+ },
+ {
+ "content": [
+ {
+ "text": "world",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ {
+ "content": [
+ {
+ "attrs": {
+ "collapsed": false,
+ },
+ "content": [
+ {
+ "content": [
+ {
+ "text": "sub item 2",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "outlineListItem",
+ },
+ {
+ "attrs": {
+ "collapsed": false,
+ },
+ "content": [
+ {
+ "content": [
+ {
+ "text": "sub item 3",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "outlineListItem",
+ },
+ ],
+ "type": "outlineList",
+ },
+ ],
+ "type": "outlineListItem",
+ },
+ {
+ "attrs": {
+ "collapsed": false,
+ },
+ "content": [
+ {
+ "content": [
+ {
+ "text": "item 4",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "outlineListItem",
+ },
+ ],
+ "type": "outlineList",
+ },
+ ],
+ "type": "doc",
+}
+`;
+
+exports[`migration > inline image in paragraph 1`] = `
+{
+ "content": [
+ {
+ "content": [
+ {
+ "text": "hello",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ {
+ "attrs": {
+ "align": undefined,
+ "aspectRatio": 1,
+ "filename": undefined,
+ "hash": undefined,
+ "height": null,
+ "mime": undefined,
+ "progress": 0,
+ "size": undefined,
+ "src": "image.png",
+ "type": "image",
+ "width": null,
+ },
+ "type": "image",
+ },
+ {
+ "content": [
+ {
+ "text": "world",
+ "type": "text",
+ },
+ ],
+ "type": "paragraph",
+ },
+ ],
+ "type": "doc",
+}
+`;
diff --git a/packages/editor/src/extensions/image/__tests__/image.test.ts b/packages/editor/src/extensions/image/__tests__/image.test.ts
new file mode 100644
index 000000000..392259e0c
--- /dev/null
+++ b/packages/editor/src/extensions/image/__tests__/image.test.ts
@@ -0,0 +1,85 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { createEditor, h, ul, li, p } from "../../../../test-utils/index.js";
+import { test, expect, describe } from "vitest";
+import { ImageNode } from "../index.js";
+import { OutlineList } from "../../outline-list/outline-list.js";
+import { OutlineListItem } from "../../outline-list-item/outline-list-item.js";
+
+function outlineList(...children: HTMLLIElement[]) {
+ return ul(children, { "data-type": "outlineList" });
+}
+
+function outlineListItem(
+ paragraphChildren: (string | HTMLElement)[],
+ subList?: HTMLUListElement
+) {
+ const children: HTMLElement[] = [h("p", paragraphChildren)];
+ if (subList) children.push(subList);
+
+ return li(children, {
+ "data-type": "outlineListItem"
+ });
+}
+
+describe("migration", () => {
+ test(`inline image in paragraph`, async () => {
+ const el = p(["hello", h("img", [], { src: "image.png" }), "world"]);
+ const {
+ builder: { image },
+ editor
+ } = createEditor({
+ initialContent: el.outerHTML,
+ extensions: {
+ image: ImageNode.configure({})
+ }
+ });
+
+ expect(editor.getJSON()).toMatchSnapshot();
+ });
+
+ test(`inline image in outline list`, async () => {
+ const el = outlineList(
+ outlineListItem(["item 1"]),
+ outlineListItem(
+ ["hello", h("img", [], { src: "image.png" }), "world"],
+ outlineList(
+ outlineListItem(["sub item 2"]),
+ outlineListItem(["sub item 3"])
+ )
+ ),
+ outlineListItem(["item 4"])
+ );
+
+ const {
+ builder: { image },
+ editor
+ } = createEditor({
+ initialContent: el.outerHTML,
+ extensions: {
+ outlineList: OutlineList,
+ outlineListItem: OutlineListItem,
+ image: ImageNode
+ }
+ });
+
+ expect(editor.getJSON()).toMatchSnapshot();
+ });
+});
diff --git a/packages/editor/src/extensions/image/image.ts b/packages/editor/src/extensions/image/image.ts
index b59554d2a..85f7a8ce7 100644
--- a/packages/editor/src/extensions/image/image.ts
+++ b/packages/editor/src/extensions/image/image.ts
@@ -131,39 +131,6 @@ export const ImageNode = Node.create<ImageOptions>({
getAttrs(node) {
if (node.querySelectorAll("img").length <= 0) return false;
return null;
- },
- getContent: (dom, schema) => {
- const wrapper = document.createElement("div");
- let buffer = "";
-
- const flushBuffer = () => {
- if (buffer.trim().length > 0) {
- const pEl = document.createElement("p");
- pEl.innerHTML = buffer;
- wrapper.appendChild(pEl);
- buffer = "";
- }
- };
-
- for (const child of dom.childNodes) {
- if (
- child.nodeType === globalThis.Node.ELEMENT_NODE &&
- (child as HTMLElement).tagName === "IMG"
- ) {
- flushBuffer();
- wrapper.appendChild(child);
- } else {
- if (child.nodeType === globalThis.Node.ELEMENT_NODE) {
- buffer += (child as HTMLElement).outerHTML;
- } else if (child.nodeType === globalThis.Node.TEXT_NODE) {
- buffer += child.textContent;
- }
- }
- }
- flushBuffer();
-
- const parser = DOMParser.fromSchema(schema);
- return parser.parse(wrapper).content;
}
},
{