From 6ebb10dc7c13d0dc96f0ab644de31a59a694b2d1 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Sat, 25 Nov 2023 14:31:29 +0800 Subject: [PATCH 1/3] feat: paste code --- packages/blocky-core/src/block/textBlock.ts | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/packages/blocky-core/src/block/textBlock.ts b/packages/blocky-core/src/block/textBlock.ts index fd9748c..fcf51af 100644 --- a/packages/blocky-core/src/block/textBlock.ts +++ b/packages/blocky-core/src/block/textBlock.ts @@ -117,7 +117,7 @@ export class TextBlock extends ContentBlock { node: container, converter, }: BlockPasteEvent): BlockDataElement | undefined { - return TextBlock.#getTextElementFromDOM( + return TextBlock.getTextElementFromDOM( editorController, container, converter @@ -127,7 +127,7 @@ export class TextBlock extends ContentBlock { /** * Rebuild the data structure from the pasted html. */ - static #getTextElementFromDOM( + static getTextElementFromDOM( editorController: EditorController, node: HTMLElement, converter: HTMLConverter @@ -158,7 +158,20 @@ export class TextBlock extends ContentBlock { if (childPtr instanceof Text) { delta.insert(removeLineBreaks(childPtr.textContent)); } else if (childPtr instanceof HTMLElement) { - if (converter.isContainerElement(childPtr)) { + const tagName = childPtr.tagName; + if (tagName === "CODE") { + delta.insert(removeLineBreaks(childPtr.textContent), { + code: true, + }); + } else if (["B", "STRONG"].includes(tagName)) { + delta.insert(removeLineBreaks(childPtr.textContent), { + bold: true, + }); + } else if (tagName === "I") { + delta.insert(removeLineBreaks(childPtr.textContent), { + italic: true, + }); + } else if (converter.isContainerElement(childPtr)) { const childElements = converter.parseContainerElement(childPtr); childrenContainer.push(...childElements); } else { From 5a6e0592e8f8697ab41fb48ad9924d7daf749af1 Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Sat, 25 Nov 2023 22:12:19 +0800 Subject: [PATCH 2/3] feat: paste bold and italic --- .../blocky-core/src/block/textBlock.spec.ts | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 packages/blocky-core/src/block/textBlock.spec.ts diff --git a/packages/blocky-core/src/block/textBlock.spec.ts b/packages/blocky-core/src/block/textBlock.spec.ts new file mode 100644 index 0000000..4fbc1fb --- /dev/null +++ b/packages/blocky-core/src/block/textBlock.spec.ts @@ -0,0 +1,99 @@ +import { describe, expect, it } from "vitest"; +import { TextBlock } from "./textBlock"; +import { HTMLConverter } from "@pkg/helper/htmlConverter"; +import { makeDefaultIdGenerator } from "@pkg/helper/idHelper"; +import { type BlockDataElement, EditorController } from ".."; + +function parseHTML(content: string): BlockDataElement { + const idGenerator = makeDefaultIdGenerator(); + const htmlConverter = new HTMLConverter({ + idGenerator, + }); + const editorController = new EditorController("user"); + + const container = document.createElement("div"); + container.innerHTML = content; + + const data = TextBlock.getTextElementFromDOM( + editorController, + container.firstChild as HTMLElement, + htmlConverter + ); + return data; +} + +describe("TextBlock", () => { + it("define", () => { + expect(TextBlock.Name).toBe("Text"); + }); + + it("paste", () => { + const data = parseHTML("

hello

"); + + expect(data.t).equals("Text"); + + const textContent = data.getTextModel("textContent"); + expect(textContent?.delta.ops).deep.equals([ + { + insert: "hello", + }, + ]); + }); + + it("paste code", () => { + const data = parseHTML("

hello world

"); + + expect(data.t).equals("Text"); + + const textContent = data.getTextModel("textContent"); + expect(textContent?.delta.ops).deep.equals([ + { + insert: "hello ", + }, + { + insert: "world", + attributes: { + code: true, + }, + }, + ]); + }); + + it("paste bold", () => { + const data = parseHTML("

hello world

"); + + expect(data.t).equals("Text"); + + const textContent = data.getTextModel("textContent"); + expect(textContent?.delta.ops).deep.equals([ + { + insert: "hello ", + }, + { + insert: "world", + attributes: { + bold: true, + }, + }, + ]); + }); + + it("paste italic", () => { + const data = parseHTML("

hello world

"); + + expect(data.t).equals("Text"); + + const textContent = data.getTextModel("textContent"); + expect(textContent?.delta.ops).deep.equals([ + { + insert: "hello ", + }, + { + insert: "world", + attributes: { + italic: true, + }, + }, + ]); + }); +}); From dbfa0ae19278cf4bca661cd9781e81331d7ec08b Mon Sep 17 00:00:00 2001 From: Vincent Chan Date: Sat, 25 Nov 2023 22:14:10 +0800 Subject: [PATCH 3/3] fix: escape error --- packages/blocky-example/app/readme.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/blocky-example/app/readme.ts b/packages/blocky-example/app/readme.ts index 527d36e..e472618 100644 --- a/packages/blocky-example/app/readme.ts +++ b/packages/blocky-example/app/readme.ts @@ -3,7 +3,7 @@ export const ReadMeContent = `

Usage

  • Click on the text to input content
  • -
  • Type \/ to trigger command panel
  • +
  • Type / to trigger command panel
  • Type @ to trigger mention panel
  • Drag the handle to re-order the blocks