Skip to content

Commit

Permalink
fix: element handling with getPageByElement in doc & edgeless (#5771)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushAgrawal-A2 authored Dec 19, 2023
1 parent d83f991 commit a769bc0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import type { Page, PageMeta } from '@blocksuite/store';
import { css, html } from 'lit';
import { customElement, property, state } from 'lit/decorators.js';

import type { DocPageBlockComponent } from '../../../../../page-block/doc/doc-page-block.js';
import type { PageBlockComponent } from '../../../../../index.js';
import { FontLinkedPageIcon, FontPageIcon } from '../../../../icons/index.js';
import {
getClosestBlockElementByElement,
getDocPageByElement,
getModelByElement,
getPageByElement,
} from '../../../../utils/index.js';
import { DEFAULT_PAGE_NAME, REFERENCE_NODE } from '../../consts.js';
import type { AffineTextAttributes } from '../types.js';
Expand Down Expand Up @@ -127,9 +127,9 @@ export class AffineReference extends WithDisposable(ShadowlessElement) {
const targetPageId = refMeta.id;
const root = model.page.root;
assertExists(root);
const docPageElement = getDocPageByElement(this) as DocPageBlockComponent;
assertExists(docPageElement);
docPageElement.slots.pageLinkClicked.emit({ pageId: targetPageId });
const pageElement = getPageByElement(this) as PageBlockComponent;
assertExists(pageElement);
pageElement.slots.pageLinkClicked.emit({ pageId: targetPageId });
}

override render() {
Expand Down
27 changes: 26 additions & 1 deletion packages/blocks/src/_common/utils/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { BaseBlockModel, Page } from '@blocksuite/store';

import type { Loader } from '../../_common/components/loader.js';
import type { RichText } from '../../_common/components/rich-text/rich-text.js';
import type { PageBlockComponent } from '../../index.js';
import type { DocPageBlockComponent } from '../../page-block/doc/doc-page-block.js';
import type { EdgelessCanvasTextEditor } from '../../page-block/edgeless/components/text/types.js';
import type { EdgelessPageBlockComponent } from '../../page-block/edgeless/edgeless-page-block.js';
Expand Down Expand Up @@ -166,6 +167,30 @@ export function buildPath(model: BaseBlockModel | null): string[] {
return path;
}

export function getPageByElement(element: Element): PageBlockComponent | null {
const docPageElement = getDocPageByElement(element);
if (docPageElement) return docPageElement;

const edgelessPageElement = getEdgelessPageByElement(element);
if (edgelessPageElement) return edgelessPageElement;

return null;
}

export function getPageByEditorHost(
editorHost: EditorHost
): PageBlockComponent | null {
if (isInsideDocEditor(editorHost)) {
return getDocPageByEditorHost(editorHost);
}

if (isInsideEdgelessEditor(editorHost)) {
return getEdgelessPageByEditorHost(editorHost);
}

return null;
}

/** If it's not in the page mode, it will return `null` directly
* Use `getDocPageByElement` or `getDocPageByEditorHost` instead.
* @deprecated
Expand Down Expand Up @@ -250,7 +275,7 @@ export function getLitRoot() {
* ```
*/
export function getViewportElement(editorHost: EditorHost) {
if (isInsideDocEditor(editorHost)) return null;
if (!isInsideDocEditor(editorHost)) return null;
const page = editorHost.page;
assertExists(page.root);
const pageComponent = editorHost.view.viewFromPath('block', [page.root.id]);
Expand Down

1 comment on commit a769bc0

@vercel
Copy link

@vercel vercel bot commented on a769bc0 Dec 19, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

blocksuite – ./packages/playground

try-blocksuite.vercel.app
blocksuite-toeverything.vercel.app
blocksuite-git-master-toeverything.vercel.app

Please sign in to comment.