Skip to content

Commit

Permalink
feat(playground): add basic/edgeless entry (#5875)
Browse files Browse the repository at this point in the history
  • Loading branch information
AyushAgrawal-A2 authored Dec 27, 2023
1 parent 2f2f1e4 commit 58d3bf4
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 18 deletions.
9 changes: 6 additions & 3 deletions packages/blocks/src/page-block/doc/doc-page-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,15 @@ export class DocPageBlockComponent extends BlockElement<
viewportUpdated: new Slot<PageViewport>(),
};

private _viewportElement: HTMLDivElement | null = null;

get viewportElement(): HTMLDivElement {
const viewportElement = this.host.closest(
if (this._viewportElement) return this._viewportElement;
this._viewportElement = this.host.closest(
'.affine-doc-viewport'
) as HTMLDivElement | null;
assertExists(viewportElement);
return viewportElement;
assertExists(this._viewportElement);
return this._viewportElement;
}

get viewport(): PageViewport {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ export class EdgelessToolbar extends WithDisposable(LitElement) {
this._timer && clearTimeout(this._timer);
document.exitFullscreen().catch(console.error);
} else {
launchIntoFullscreen(this.edgeless.editorContainer);
launchIntoFullscreen(this.edgeless.viewportElement);
this._timer = setTimeout(() => {
this._currentFrameIndex = this._cachedIndex;
}, 400);
Expand Down
19 changes: 10 additions & 9 deletions packages/blocks/src/page-block/edgeless/edgeless-page-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ import {
import { xywhArrayToObject } from './utils/convert.js';
import { getCursorMode, isCanvasElement, isFrameBlock } from './utils/query.js';

type EdtitorContainer = HTMLElement & { mode: 'page' | 'edgeless' };

@customElement('affine-edgeless-page')
export class EdgelessPageBlockComponent extends BlockElement<
PageBlockModel,
Expand Down Expand Up @@ -221,14 +219,17 @@ export class EdgelessPageBlockComponent extends BlockElement<
return this.service?.uiEventDispatcher;
}

private _editorContainer: EdtitorContainer | null = null;
private _viewportElement: HTMLElement | null = null;

get editorContainer(): EdtitorContainer {
if (this._editorContainer) return this._editorContainer;
this._editorContainer = this.closest('affine-editor-container');
assertExists(this._editorContainer);
return this._editorContainer;
get viewportElement(): HTMLElement {
if (this._viewportElement) return this._viewportElement;
this._viewportElement = this.host.closest(
'edgeless-editor'
) as HTMLElement | null;
assertExists(this._viewportElement);
return this._viewportElement;
}

private _resizeObserver: ResizeObserver | null = null;

get surfaceBlockModel() {
Expand Down Expand Up @@ -587,7 +588,7 @@ export class EdgelessPageBlockComponent extends BlockElement<
this.selectionManager.set(this.selectionManager.selections);
});

resizeObserver.observe(this.editorContainer);
resizeObserver.observe(this.viewportElement);
this._resizeObserver = resizeObserver;
}

Expand Down
15 changes: 12 additions & 3 deletions packages/blocks/src/surface-block/surface-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
type CssVariableName,
isCssVariable,
} from '../_common/theme/css-variables.js';
import { getThemePropertyValue } from '../_common/theme/utils.js';
import { ThemeObserver } from '../_common/theme/theme-observer.js';
import {
type EdgelessElement,
isInsideEdgelessEditor,
Expand Down Expand Up @@ -166,6 +166,8 @@ export class SurfaceBlockComponent extends BlockElement<
private _lastTime = 0;
private _cachedViewport = new Bound();

private readonly _themeObserver = new ThemeObserver();

@query('edgeless-block-portal-container')
portal!: EdgelessBlockPortalContainer;

Expand Down Expand Up @@ -259,12 +261,13 @@ export class SurfaceBlockComponent extends BlockElement<
this._initEvents();
this.layer.init([...this._elements.values(), ...this.blocks]);
this.init();
this._initThemeObserver();
}

getCSSPropertyValue = (value: string) => {
const root = this.host;
if (isCssVariable(value)) {
const cssValue = getThemePropertyValue(root, value as CssVariableName);
const cssValue =
this._themeObserver.cssVariables?.[value as CssVariableName];
if (cssValue === undefined) {
console.error(
new Error(
Expand Down Expand Up @@ -334,6 +337,12 @@ export class SurfaceBlockComponent extends BlockElement<
);
}

private _initThemeObserver = () => {
this._themeObserver.observe(document.documentElement);
this._themeObserver.on(() => this.requestUpdate());
this.disposables.add(() => this._themeObserver.dispose());
};

private _getSortedSameGroupElements(element: EdgelessElement) {
let elements: EdgelessElement[];
const group = this.getGroupParent(element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Basic DocEditor Example</title>
<style>
html,
body {
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
background-color: var(--affine-white-90);
transition: background-color 0.3s;
}
</style>
</head>
<body>
<script type="module" src="./main.ts"></script>
Expand Down
File renamed without changes.
23 changes: 23 additions & 0 deletions packages/playground/examples/basic/edgeless/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Basic EdgelessEditor Example</title>
<style>
html,
body {
height: 100%;
overflow: hidden;
margin: 0;
padding: 0;
background-color: var(--affine-white-90);
transition: background-color 0.3s;
}
</style>
</head>
<body>
<script type="module" src="./main.ts"></script>
</body>
</html>
9 changes: 9 additions & 0 deletions packages/playground/examples/basic/edgeless/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import '@blocksuite/presets/themes/affine.css';

import { createEmptyPage, EdgelessEditor } from '@blocksuite/presets';

const page = createEmptyPage().init();
const editor = new EdgelessEditor();
editor.page = page;
document.body.appendChild(editor);
2 changes: 1 addition & 1 deletion packages/playground/vite.config.size.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const config = mergeConfig(
);

config.build!.rollupOptions!.input = {
main: resolve(__dirname, 'examples/basic/index.html'),
main: resolve(__dirname, 'examples/basic/doc/index.html'),
};

export default config;
9 changes: 8 additions & 1 deletion packages/playground/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,14 @@ export default ({ mode }) => {
input: {
main: resolve(__dirname, 'index.html'),
'starter/': resolve(__dirname, 'starter/index.html'),
'examples/basic': resolve(__dirname, 'examples/basic/index.html'),
'examples/basic/doc': resolve(
__dirname,
'examples/basic/doc/index.html'
),
'examples/basic/edgeless': resolve(
__dirname,
'examples/basic/edgeless/index.html'
),
'examples/inline': resolve(__dirname, 'examples/inline/index.html'),
'examples/store': resolve(__dirname, 'examples/store/index.html'),
},
Expand Down

1 comment on commit 58d3bf4

@vercel
Copy link

@vercel vercel bot commented on 58d3bf4 Dec 27, 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-git-master-toeverything.vercel.app
blocksuite-toeverything.vercel.app

Please sign in to comment.