`;
+ override async block2html({ editor, selectInfo, block }: Block2HtmlProps) {
+ const childrenHtml =
+ await editor.clipboard.clipboardUtils.convertBlock2HtmlBySelectInfos(
+ block,
+ selectInfo?.children
+ );
+ return `
${childrenHtml}
`;
}
}
diff --git a/libs/components/editor-blocks/src/blocks/groupDvider/index.ts b/libs/components/editor-blocks/src/blocks/groupDvider/index.ts
index 4120f1a525..458ef2f708 100644
--- a/libs/components/editor-blocks/src/blocks/groupDvider/index.ts
+++ b/libs/components/editor-blocks/src/blocks/groupDvider/index.ts
@@ -5,35 +5,13 @@ import {
} from '@toeverything/framework/virgo';
import { Protocol } from '@toeverything/datasource/db-service';
import { GroupDividerView } from './groupDividerView';
+import { Block2HtmlProps } from '../../utils/commonBlockClip';
export class GroupDividerBlock extends BaseView {
type = Protocol.Block.Type.groupDivider;
View = GroupDividerView;
- override html2block(
- el: Element,
- parseEl: (el: Element) => any[]
- ): any[] | null {
- const tag_name = el.tagName;
- if (tag_name === 'HR') {
- return [
- {
- type: this.type,
- properties: {
- text: {},
- },
- children: [],
- },
- ];
- }
- return null;
- }
-
- override async block2html(
- block: AsyncBlock,
- children: SelectBlock[],
- generateHtml: (el: any[]) => Promise
- ): Promise {
- return `
`;
+ override async block2html(props: Block2HtmlProps) {
+ return `
`;
}
}
diff --git a/libs/components/editor-blocks/src/blocks/image/index.ts b/libs/components/editor-blocks/src/blocks/image/index.ts
index e5c33514e2..b170d44413 100644
--- a/libs/components/editor-blocks/src/blocks/image/index.ts
+++ b/libs/components/editor-blocks/src/blocks/image/index.ts
@@ -1,10 +1,12 @@
import {
- AsyncBlock,
BaseView,
- SelectBlock,
+ BlockEditor,
+ HTML2BlockResult,
} from '@toeverything/framework/virgo';
import { Protocol } from '@toeverything/datasource/db-service';
import { ImageView } from './ImageView';
+import { Block2HtmlProps } from '../../utils/commonBlockClip';
+import { getRandomString } from '@toeverything/components/common';
export class ImageBlock extends BaseView {
public override selectable = true;
@@ -13,42 +15,40 @@ export class ImageBlock extends BaseView {
View = ImageView;
// TODO: needs to download the image and then upload it to get a new link and then assign it
- override html2block(
- el: Element,
- parseEl: (el: Element) => any[]
- ): any[] | null {
- const tag_name = el.tagName;
- if (tag_name === 'IMG') {
+ override async html2block({
+ element,
+ editor,
+ }: {
+ element: Element;
+ editor: BlockEditor;
+ }): Promise {
+ if (element.tagName === 'IMG') {
return [
{
type: this.type,
properties: {
- value: '',
- url: el.getAttribute('src'),
- name: el.getAttribute('src'),
- size: 0,
- type: 'link',
+ image: {
+ value: getRandomString('image'),
+ url: element.getAttribute('src'),
+ name: element.getAttribute('src'),
+ size: 0,
+ type: 'link',
+ },
},
children: [],
},
];
}
-
return null;
}
- // TODO:
- override async block2html(
- block: AsyncBlock,
- children: SelectBlock[],
- generateHtml: (el: any[]) => Promise
- ): Promise {
- const text = block.getProperty('text');
+ override async block2html({ block, editor }: Block2HtmlProps) {
+ const textValue = block.getProperty('text');
const content = '';
- if (text) {
- text.value.map(text => `${text}`).join('');
- }
- // TODO: child
- return `
`;
+ // TODO: text.value should export with style??
+ const figcaption = (textValue?.value ?? [])
+ .map(({ text }) => `${text}`)
+ .join('');
+ return `
${figcaption}`;
}
}
diff --git a/libs/components/editor-blocks/src/blocks/numbered/index.ts b/libs/components/editor-blocks/src/blocks/numbered/index.ts
index 92187be81b..3dd6f74934 100644
--- a/libs/components/editor-blocks/src/blocks/numbered/index.ts
+++ b/libs/components/editor-blocks/src/blocks/numbered/index.ts
@@ -1,15 +1,15 @@
-import {
- DefaultColumnsValue,
- Protocol,
-} from '@toeverything/datasource/db-service';
+import { Protocol } from '@toeverything/datasource/db-service';
import {
AsyncBlock,
BaseView,
- getTextHtml,
- getTextProperties,
- SelectBlock,
+ BlockEditor,
+ HTML2BlockResult,
} from '@toeverything/framework/virgo';
-// import { withTreeViewChildren } from '../../utils/with-tree-view-children';
+import {
+ Block2HtmlProps,
+ commonBlock2HtmlContent,
+ commonHTML2block,
+} from '../../utils/commonBlockClip';
import { defaultTodoProps, NumberedView } from './NumberedView';
export class NumberedBlock extends BaseView {
@@ -28,71 +28,39 @@ export class NumberedBlock extends BaseView {
return block;
}
- override getSelProperties(
- block: AsyncBlock,
- selectInfo: any
- ): DefaultColumnsValue {
- const properties = super.getSelProperties(block, selectInfo);
- return getTextProperties(properties, selectInfo);
- }
-
- override html2block(
- el: Element,
- parseEl: (el: Element) => any[]
- ): any[] | null {
- const tag_name = el.tagName;
- if (tag_name === 'OL') {
- const result = [];
- for (let i = 0; i < el.children.length; i++) {
- const blocks_info = parseEl(el.children[i]);
- result.push(...blocks_info);
- }
- return result.length > 0 ? result : null;
+ override async html2block({
+ element,
+ editor,
+ }: {
+ element: Element;
+ editor: BlockEditor;
+ }): Promise {
+ if (element.tagName === 'OL') {
+ const children = Array.from(element.children);
+ const childrenBlockInfos = (
+ await Promise.all(
+ children.map(childElement =>
+ this.html2block({
+ editor,
+ element: childElement,
+ })
+ )
+ )
+ )
+ .flat()
+ .filter(v => v);
+ return childrenBlockInfos.length ? childrenBlockInfos : null;
}
- if (tag_name == 'LI' && el.textContent.startsWith('[ ] ')) {
- const childNodes = el.childNodes;
- let texts = [];
- const children = [];
- for (let i = 0; i < childNodes.length; i++) {
- const blocks_info = parseEl(childNodes[i] as Element);
- for (let j = 0; j < blocks_info.length; j++) {
- if (blocks_info[j].type === 'text') {
- const block_texts =
- blocks_info[j].properties.text.value;
- texts.push(...block_texts);
- } else {
- children.push(blocks_info[j]);
- }
- }
- }
- if (texts.length > 0 && (texts[0].text || '').startsWith('[ ] ')) {
- texts[0].text = texts[0].text.substring('[ ] '.length);
- if (!texts[0].text) {
- texts = texts.slice(1);
- }
- }
- return [
- {
- type: this.type,
- properties: {
- text: { value: texts },
- },
- children: children,
- },
- ];
- }
-
- return null;
+ return commonHTML2block({
+ element,
+ editor,
+ type: this.type,
+ tagName: 'LI',
+ });
}
- override async block2html(
- block: AsyncBlock,
- children: SelectBlock[],
- generateHtml: (el: any[]) => Promise
- ): Promise {
- let content = getTextHtml(block);
- content += await generateHtml(children);
- return `- ${content}
`;
+ override async block2html(props: Block2HtmlProps) {
+ return `- ${await commonBlock2HtmlContent(props)}
`;
}
}
diff --git a/libs/components/editor-blocks/src/blocks/page/index.ts b/libs/components/editor-blocks/src/blocks/page/index.ts
index 412bbdbd7c..4209cbbdb6 100644
--- a/libs/components/editor-blocks/src/blocks/page/index.ts
+++ b/libs/components/editor-blocks/src/blocks/page/index.ts
@@ -1,21 +1,9 @@
import { withRecastBlock } from '@toeverything/components/editor-core';
-import {
- Protocol,
- DefaultColumnsValue,
-} from '@toeverything/datasource/db-service';
-import {
- AsyncBlock,
- BaseView,
- ChildrenView,
- getTextHtml,
- getTextProperties,
- SelectBlock,
-} from '@toeverything/framework/virgo';
+import { Protocol } from '@toeverything/datasource/db-service';
+import { AsyncBlock, BaseView } from '@toeverything/framework/virgo';
import { PageView } from './PageView';
-
-export const PageChildrenView: (prop: ChildrenView) => JSX.Element = props =>
- props.children;
+import { Block2HtmlProps } from '../../utils/commonBlockClip';
export class PageBlock extends BaseView {
type = Protocol.Block.Type.page;
@@ -35,21 +23,17 @@ export class PageBlock extends BaseView {
return this.get_decoration(content, 'text')?.value?.[0].text;
}
- override getSelProperties(
- block: AsyncBlock,
- selectInfo: any
- ): DefaultColumnsValue {
- const properties = super.getSelProperties(block, selectInfo);
- return getTextProperties(properties, selectInfo);
- }
-
- override async block2html(
- block: AsyncBlock,
- children: SelectBlock[],
- generateHtml: (el: any[]) => Promise
- ): Promise {
- const content = getTextHtml(block);
- const childrenContent = await generateHtml(children);
- return `${content}
${childrenContent}`;
+ override async block2html({ block, editor, selectInfo }: Block2HtmlProps) {
+ const header =
+ await editor.clipboard.clipboardUtils.convertTextValue2HtmlBySelectInfo(
+ block,
+ selectInfo
+ );
+ const childrenHtml =
+ await editor.clipboard.clipboardUtils.convertBlock2HtmlBySelectInfos(
+ block,
+ selectInfo?.children
+ );
+ return `${header}
${childrenHtml}`;
}
}
diff --git a/libs/components/editor-blocks/src/blocks/text/QuoteBlock.tsx b/libs/components/editor-blocks/src/blocks/text/QuoteBlock.tsx
index f62dfae040..7789ec5f1c 100644
--- a/libs/components/editor-blocks/src/blocks/text/QuoteBlock.tsx
+++ b/libs/components/editor-blocks/src/blocks/text/QuoteBlock.tsx
@@ -1,15 +1,16 @@
-import {
- DefaultColumnsValue,
- Protocol,
-} from '@toeverything/datasource/db-service';
+import { Protocol } from '@toeverything/datasource/db-service';
import {
AsyncBlock,
BaseView,
+ BlockEditor,
CreateView,
- getTextHtml,
- getTextProperties,
- SelectBlock,
+ HTML2BlockResult,
} from '@toeverything/framework/virgo';
+import {
+ Block2HtmlProps,
+ commonBlock2HtmlContent,
+ commonHTML2block,
+} from '../../utils/commonBlockClip';
import { TextView } from './TextView';
@@ -29,54 +30,25 @@ export class QuoteBlock extends BaseView {
return block;
}
- override getSelProperties(
- block: AsyncBlock,
- selectInfo: any
- ): DefaultColumnsValue {
- const properties = super.getSelProperties(block, selectInfo);
- return getTextProperties(properties, selectInfo);
+ override async html2block({
+ element,
+ editor,
+ }: {
+ element: Element;
+ editor: BlockEditor;
+ }): Promise {
+ return commonHTML2block({
+ element,
+ editor,
+ type: this.type,
+ tagName: 'BLOCKQUOTE',
+ });
}
- override html2block(
- el: Element,
- parseEl: (el: Element) => any[]
- ): any[] | null {
- const tag_name = el.tagName;
- if (tag_name === 'BLOCKQUOTE') {
- const childNodes = el.childNodes;
- const texts = [];
- for (let i = 0; i < childNodes.length; i++) {
- const blocks_info = parseEl(childNodes[i] as Element);
- for (let j = 0; j < blocks_info.length; j++) {
- if (blocks_info[j].type === 'text') {
- const block_texts =
- blocks_info[j].properties.text.value;
- texts.push(...block_texts);
- }
- }
- }
- return [
- {
- type: this.type,
- properties: {
- text: { value: texts },
- },
- children: [],
- },
- ];
- }
-
- return null;
- }
-
- override async block2html(
- block: AsyncBlock,
- children: SelectBlock[],
- generateHtml: (el: any[]) => Promise
- ): Promise {
- let content = getTextHtml(block);
- content += await generateHtml(children);
- return `${content}
`;
+ override async block2html(props: Block2HtmlProps) {
+ return `${await commonBlock2HtmlContent(
+ props
+ )}
`;
}
}
@@ -96,69 +68,22 @@ export class CalloutBlock extends BaseView {
return block;
}
- override getSelProperties(
- block: AsyncBlock,
- selectInfo: any
- ): DefaultColumnsValue {
- const properties = super.getSelProperties(block, selectInfo);
- return getTextProperties(properties, selectInfo);
+ override async html2block({
+ element,
+ editor,
+ }: {
+ element: Element;
+ editor: BlockEditor;
+ }): Promise {
+ return commonHTML2block({
+ element,
+ editor,
+ type: this.type,
+ tagName: 'ASIDE',
+ });
}
- override html2block(
- el: Element,
- parseEl: (el: Element) => any[]
- ): any[] | null {
- const tag_name = el.tagName;
- if (
- tag_name === 'ASIDE' ||
- el.firstChild?.nodeValue?.startsWith('