Skip to content

Commit

Permalink
Feat/paste code (#88)
Browse files Browse the repository at this point in the history
* feat: paste code

* feat: paste bold and italic

* fix: escape error
  • Loading branch information
vincentdchan authored Nov 25, 2023
1 parent f15b7ba commit f1421ea
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 4 deletions.
99 changes: 99 additions & 0 deletions packages/blocky-core/src/block/textBlock.spec.ts
Original file line number Diff line number Diff line change
@@ -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("<p>hello</p>");

expect(data.t).equals("Text");

const textContent = data.getTextModel("textContent");
expect(textContent?.delta.ops).deep.equals([
{
insert: "hello",
},
]);
});

it("paste code", () => {
const data = parseHTML("<p>hello <code>world</code></p>");

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("<p>hello <b>world</b></p>");

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("<p>hello <i>world</i></p>");

expect(data.t).equals("Text");

const textContent = data.getTextModel("textContent");
expect(textContent?.delta.ops).deep.equals([
{
insert: "hello ",
},
{
insert: "world",
attributes: {
italic: true,
},
},
]);
});
});
19 changes: 16 additions & 3 deletions packages/blocky-core/src/block/textBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export class TextBlock extends ContentBlock {
node: container,
converter,
}: BlockPasteEvent): BlockDataElement | undefined {
return TextBlock.#getTextElementFromDOM(
return TextBlock.getTextElementFromDOM(
editorController,
container,
converter
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/blocky-example/app/readme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const ReadMeContent = `
<h2>Usage</h2>
<ul>
<li>Click on the text to input content</li>
<li>Type <code>\/</code> to trigger command panel</li>
<li>Type <code>/</code> to trigger command panel</li>
<li>Type <code>@</code> to trigger mention panel</li>
<li>Drag the handle to re-order the blocks</li>
</ul>
Expand Down

1 comment on commit f1421ea

@vercel
Copy link

@vercel vercel bot commented on f1421ea Nov 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.