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
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, + }, + }, + ]); + }); +}); 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 { 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 = `\/
to trigger command panel/
to trigger command panel@
to trigger mention panel