forked from toeverything/blocksuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
135 lines (119 loc) Β· 3.86 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/// <reference types="./env" />
import '@blocksuite/blocks';
import '@blocksuite/editor';
import './components/start-panel';
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
import '@blocksuite/editor/themes/affine.css';
import { ContentParser } from '@blocksuite/blocks/content-parser';
import { __unstableSchemas, AffineSchemas } from '@blocksuite/blocks/models';
import std from '@blocksuite/blocks/std';
import type { DocProvider, Page } from '@blocksuite/store';
import { Workspace } from '@blocksuite/store';
import { DebugMenu } from './components/debug-menu.js';
import type { InitFn } from './data';
import {
createEditor,
createWorkspaceOptions,
defaultMode,
initDebugConfig,
initParam,
isE2E,
tryInitExternalContent,
} from './utils.js';
const options = createWorkspaceOptions();
initDebugConfig();
// Subscribe for page update and create editor after page loaded.
function subscribePage(workspace: Workspace) {
workspace.slots.pageAdded.once(pageId => {
if (typeof globalThis.targetPageId === 'string') {
if (pageId !== globalThis.targetPageId) {
// if there's `targetPageId` which not same as the `pageId`
return;
}
}
const app = document.getElementById('app');
if (!app) {
return;
}
const page = workspace.getPage(pageId) as Page;
const editor = createEditor(page, app);
const contentParser = new ContentParser(page);
const debugMenu = new DebugMenu();
debugMenu.workspace = workspace;
debugMenu.editor = editor;
debugMenu.mode = defaultMode;
debugMenu.contentParser = contentParser;
document.body.appendChild(debugMenu);
window.editor = editor;
window.page = page;
});
}
export async function initPageContentByParam(
workspace: Workspace,
param: string,
pageId: string
) {
const functionMap = new Map<
string,
(workspace: Workspace, id: string) => void
>();
Object.values(
(await import('./data/index.js')) as Record<string, InitFn>
).forEach(fn => functionMap.set(fn.id, fn));
// Load the preset playground documentation when `?init` param provided
if (param === '') {
param = 'preset';
}
// Load built-in init function when `?init=heavy` param provided
if (functionMap.has(param)) {
functionMap.get(param)?.(workspace, pageId);
const page = workspace.getPage(pageId);
await page?.waitForLoaded();
page?.resetHistory();
return;
}
// Try to load base64 content or markdown content from url
await tryInitExternalContent(workspace, param, pageId);
}
async function main() {
if (window.workspace) {
return;
}
const workspace = new Workspace(options)
.register(AffineSchemas)
.register(__unstableSchemas);
window.workspace = workspace;
window.blockSchemas = AffineSchemas;
window.Y = Workspace.Y;
window.std = std;
window.ContentParser = ContentParser;
workspace.awarenessStore.setFlag('enable_page_tags', true);
const syncProviders = async (providers: DocProvider[]) => {
for (const provider of providers) {
if ('active' in provider) {
provider.sync();
await provider.whenReady;
} else if ('passive' in provider) {
provider.connect();
}
}
};
await syncProviders(workspace.providers);
workspace.slots.pageAdded.on(async pageId => {
const page = workspace.getPage(pageId) as Page;
await page.waitForLoaded();
});
// In E2E environment, initial state should be generated by test case,
// instead of using this default setup.
if (isE2E) return;
subscribePage(workspace);
if (initParam !== null) {
await initPageContentByParam(workspace, initParam, 'page0');
return;
}
// Open default examples list when no `?init` param is provided
const exampleList = document.createElement('start-panel');
workspace.slots.pageAdded.once(() => exampleList.remove());
document.body.prepend(exampleList);
}
main();