feat(ios): add markdown import api (#11394)
This commit is contained in:
@@ -26,12 +26,19 @@ import {
|
|||||||
} from '@affine/core/modules/storage';
|
} from '@affine/core/modules/storage';
|
||||||
import { PopupWindowProvider } from '@affine/core/modules/url';
|
import { PopupWindowProvider } from '@affine/core/modules/url';
|
||||||
import { ClientSchemeProvider } from '@affine/core/modules/url/providers/client-schema';
|
import { ClientSchemeProvider } from '@affine/core/modules/url/providers/client-schema';
|
||||||
import { configureBrowserWorkbenchModule } from '@affine/core/modules/workbench';
|
import {
|
||||||
import { WorkspacesService } from '@affine/core/modules/workspace';
|
configureBrowserWorkbenchModule,
|
||||||
|
WorkbenchService,
|
||||||
|
} from '@affine/core/modules/workbench';
|
||||||
|
import {
|
||||||
|
getAFFiNEWorkspaceSchema,
|
||||||
|
WorkspacesService,
|
||||||
|
} from '@affine/core/modules/workspace';
|
||||||
import { configureBrowserWorkspaceFlavours } from '@affine/core/modules/workspace-engine';
|
import { configureBrowserWorkspaceFlavours } from '@affine/core/modules/workspace-engine';
|
||||||
import { I18n } from '@affine/i18n';
|
import { I18n } from '@affine/i18n';
|
||||||
import { StoreManagerClient } from '@affine/nbstore/worker/client';
|
import { StoreManagerClient } from '@affine/nbstore/worker/client';
|
||||||
import { defaultBlockMarkdownAdapterMatchers } from '@blocksuite/affine/adapters';
|
import { defaultBlockMarkdownAdapterMatchers } from '@blocksuite/affine/adapters';
|
||||||
|
import { MarkdownTransformer } from '@blocksuite/affine/blocks/root';
|
||||||
import { Container } from '@blocksuite/affine/global/di';
|
import { Container } from '@blocksuite/affine/global/di';
|
||||||
import {
|
import {
|
||||||
InlineDeltaToMarkdownAdapterExtensions,
|
InlineDeltaToMarkdownAdapterExtensions,
|
||||||
@@ -282,6 +289,45 @@ const frameworkProvider = framework.provider();
|
|||||||
disposeWorkspace();
|
disposeWorkspace();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
(window as any).createNewDocByMarkdownInCurrentWorkspace = async (
|
||||||
|
markdown: string,
|
||||||
|
title: string
|
||||||
|
) => {
|
||||||
|
const globalContextService = frameworkProvider.get(GlobalContextService);
|
||||||
|
const currentWorkspaceId =
|
||||||
|
globalContextService.globalContext.workspaceId.get();
|
||||||
|
const workspacesService = frameworkProvider.get(WorkspacesService);
|
||||||
|
const workspaceRef = currentWorkspaceId
|
||||||
|
? workspacesService.openByWorkspaceId(currentWorkspaceId)
|
||||||
|
: null;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const workspace = workspaceRef?.workspace;
|
||||||
|
if (!workspace) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const workbench = workspace.scope.get(WorkbenchService).workbench;
|
||||||
|
await workspace.engine.doc.waitForDocReady(workspace.id); // wait for root doc ready
|
||||||
|
const docId = await MarkdownTransformer.importMarkdownToDoc({
|
||||||
|
collection: workspace.docCollection,
|
||||||
|
schema: getAFFiNEWorkspaceSchema(),
|
||||||
|
markdown,
|
||||||
|
});
|
||||||
|
const docsService = workspace.scope.get(DocsService);
|
||||||
|
if (docId) {
|
||||||
|
// only support page mode for now
|
||||||
|
await docsService.changeDocTitle(docId, title);
|
||||||
|
docsService.list.setPrimaryMode(docId, 'page');
|
||||||
|
workbench.openDoc(docId);
|
||||||
|
return docId;
|
||||||
|
} else {
|
||||||
|
throw new Error('Failed to import doc');
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
workspaceRef?.dispose();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// setup application lifecycle events, and emit application start event
|
// setup application lifecycle events, and emit application start event
|
||||||
window.addEventListener('focus', () => {
|
window.addEventListener('focus', () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user