Skip to content

Commit

Permalink
fix: export png error and rich text format confusion (toeverything#5945)
Browse files Browse the repository at this point in the history
  • Loading branch information
donteatfriedrice authored Jan 9, 2024
1 parent a2678cd commit d54a817
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 33 deletions.
22 changes: 13 additions & 9 deletions packages/blocks/src/_common/export-manager/export-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { EditorHost } from '@blocksuite/lit';
import type { BlockModel, Page } from '@blocksuite/store';

import {
blockElementGetter,
getBlockComponentByModel,
getEditorContainer,
isInsideDocEditor,
Expand Down Expand Up @@ -180,10 +181,10 @@ export class ExportManager {
public async edgelessToCanvas(
surfaceRenderer: Renderer,
bound: IBound,
blockElementGetter: (model: BlockModel) => Element | null = () => null,
edgeless?: EdgelessPageBlockComponent,
nodes?: TopLevelBlockModel[],
surfaces?: SurfaceElement[],
blockElementGetter: (model: BlockModel) => Element | null = () => null,
edgelessBackground?: {
zoom: number;
}
Expand Down Expand Up @@ -248,7 +249,10 @@ export class ExportManager {
blockBound.h
);
}
const blockElement = blockElementGetter(block)?.parentElement;
let blockElement = blockElementGetter(block)?.parentElement;
if (matchFlavours(block, ['affine:note'])) {
blockElement = blockElement?.closest('.edgeless-block-portal-note');
}

if (blockElement) {
const blockBound = xywhArrayToObject(block);
Expand All @@ -268,7 +272,7 @@ export class ExportManager {

for (let i = 0; i < blocksInsideFrame.length; i++) {
const element = blocksInsideFrame[i];
const htmlElement = blockElementGetter(element)?.parentElement;
const htmlElement = blockElementGetter(element);
const blockBound = xywhArrayToObject(element);
const canvasData = await html2canvas(htmlElement as HTMLElement);

Expand Down Expand Up @@ -302,10 +306,9 @@ export class ExportManager {
const pageMode = isInsideDocEditor(this.editorHost);

const editorContainer = getEditorContainer(this.editorHost);
const pageContainer = editorContainer.querySelector(
'.affine-doc-page-block-container'
);
if (!pageContainer) return;
const docEditorContainer =
editorContainer.querySelector('affine-doc-editor');
if (!docEditorContainer) return;

const replaceRichTextWithSvgElementFunc =
this._replaceRichTextWithSvgElement.bind(this);
Expand Down Expand Up @@ -340,15 +343,15 @@ export class ExportManager {
};

const data = await html2canvas(
pageContainer as HTMLElement,
docEditorContainer as HTMLElement,
html2canvasOption
);
this._checkCanContinueToCanvas(pathname, pageMode);
return data;
}

private _replaceRichTextWithSvgElement = async (element: HTMLElement) => {
const richList = Array.from(element.querySelectorAll('rich-text'));
const richList = Array.from(element.querySelectorAll('.inline-editor'));
await Promise.all(
richList.map(async rich => {
const svgEle = await this._elementToSvgElement(
Expand Down Expand Up @@ -413,6 +416,7 @@ export class ExportManager {
return await this.edgelessToCanvas(
edgeless.surface.viewport,
bound,
(model: BlockModel) => blockElementGetter(model, this.editorHost.view),
edgeless
);
}
Expand Down
19 changes: 19 additions & 0 deletions packages/blocks/src/_common/utils/query.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ViewStore } from '@blocksuite/block-std';
import { assertExists } from '@blocksuite/global/utils';
import type { InlineEditor } from '@blocksuite/inline';
import { INLINE_ROOT_ATTR } from '@blocksuite/inline';
Expand Down Expand Up @@ -168,6 +169,24 @@ export function buildPath(model: BlockModel | null): string[] {
return path;
}

export function blockElementGetter(model: BlockModel, view: ViewStore) {
if (matchFlavours(model, ['affine:image', 'affine:frame'])) {
let current: BlockModel | null = model;
const path: string[] = [];
while (current) {
// Top level image render under page block not surface block
if (!matchFlavours(current, ['affine:surface'])) {
path.unshift(current.id);
}
current = current.page.getParent(current);
}

return view.viewFromPath('block', path);
} else {
return view.viewFromPath('block', buildPath(model));
}
}

export function getPageByElement(element: Element): PageBlockComponent | null {
const docPageElement = getDocPageByElement(element);
if (docPageElement) return docPageElement;
Expand Down
26 changes: 4 additions & 22 deletions packages/blocks/src/page-block/edgeless/controllers/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import type {
} from '@blocksuite/block-std';
import { assertExists, DisposableGroup } from '@blocksuite/global/utils';
import type { EditorHost } from '@blocksuite/lit';
import type { BlockModel } from '@blocksuite/store';
import {
type BlockSnapshot,
fromJSON,
Expand All @@ -21,7 +20,7 @@ import type {
import { matchFlavours } from '../../../_common/utils/index.js';
import { groupBy } from '../../../_common/utils/iterable.js';
import {
buildPath,
blockElementGetter,
getEditorContainer,
isInsideDocEditor,
} from '../../../_common/utils/query.js';
Expand Down Expand Up @@ -128,24 +127,6 @@ export class EdgelessClipboardController extends PageClipboard {
);
};

private _blockElmentGetter(model: BlockModel) {
if (matchFlavours(model, ['affine:image', 'affine:frame'])) {
let current: BlockModel | null = model;
const path: string[] = [];
while (current) {
// Top level image render under page block not surface block
if (!matchFlavours(current, ['affine:surface'])) {
path.unshift(current.id);
}
current = current.page.getParent(current);
}

return this.std.view.viewFromPath('block', path);
} else {
return this.std.view.viewFromPath('block', buildPath(model));
}
}

private _onCopy = async (
_context: UIEventStateContext,
surfaceSelection: SurfaceSelection[]
Expand Down Expand Up @@ -625,7 +606,7 @@ export class EdgelessClipboardController extends PageClipboard {
}

private async _replaceRichTextWithSvgElement(element: HTMLElement) {
const richList = Array.from(element.querySelectorAll('rich-text'));
const richList = Array.from(element.querySelectorAll('.inline-editor'));
await Promise.all(
richList.map(async rich => {
const svgEle = await this._elementToSvgElement(
Expand Down Expand Up @@ -749,7 +730,8 @@ export class EdgelessClipboardController extends PageClipboard {
block: TopLevelBlockModel,
isInFrame = false
) => {
let blockElement = this._blockElmentGetter(block)?.parentElement;
let blockElement = blockElementGetter(block, this.std.view)
?.parentElement;
const blockPortalSelector = block.flavour.replace(
'affine:',
'.edgeless-block-portal-'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ export const edgelessToBlob = async (
.edgelessToCanvas(
options.surfaceRenderer,
bound,
model =>
blockContainer.querySelector(
`[data-portal-reference-block-id="${model.id}"]`
),
undefined,
isBlock ? [edgelessElement] : undefined,
isBlock ? undefined : [edgelessElement],
id => blockContainer.querySelector(`[portal-reference-block-id="${id}"]`),
{ zoom: options.surfaceRenderer.zoom }
)
.then(canvas => {
Expand Down
7 changes: 6 additions & 1 deletion packages/blocks/src/surface-ref-block/portal/image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,12 @@ export class SurfaceRefImagePortal extends WithDisposable(ShadowlessElement) {
};

return html`
<div style=${styleMap(style)}>${this.renderModel(model)}</div>
<div
style=${styleMap(style)}
data-portal-reference-block-id="${model.id}"
>
${this.renderModel(model)}
</div>
`;
}
}
Expand Down
1 change: 1 addition & 0 deletions packages/blocks/src/surface-ref-block/portal/note.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export class SurfaceRefNotePortal extends WithDisposable(ShadowlessElement) {
class="surface-ref-note-portal"
style=${styleMap(style)}
data-model-height="${modelH}"
data-portal-reference-block-id="${model.id}"
>
${this.renderModel(model)}
</div>
Expand Down

0 comments on commit d54a817

Please sign in to comment.