feat(mobile): get content in markdown api

This commit is contained in:
EYHN 2024-11-19 14:36:52 +08:00
parent 442c86011a
commit b28b555d80
No known key found for this signature in database
GPG Key ID: 936F232D66B3E1FE
7 changed files with 74 additions and 8 deletions

View File

@ -66,6 +66,7 @@ export function configureWorkspaceModule(framework: Framework) {
.service(WorkspaceRepositoryService, [
[WorkspaceFlavourProvider],
WorkspaceProfileService,
WorkspaceListService,
])
.scope(WorkspaceScope)
.service(WorkspaceService)

View File

@ -11,6 +11,7 @@ import type {
WorkspaceFlavourProvider,
} from '../providers/flavour';
import { WorkspaceScope } from '../scopes/workspace';
import type { WorkspaceListService } from './list';
import type { WorkspaceProfileService } from './profile';
import { WorkspaceService } from './workspace';
@ -19,7 +20,8 @@ const logger = new DebugLogger('affine:workspace-repository');
export class WorkspaceRepositoryService extends Service {
constructor(
private readonly providers: WorkspaceFlavourProvider[],
private readonly profileRepo: WorkspaceProfileService
private readonly profileRepo: WorkspaceProfileService,
private readonly workspacesListService: WorkspaceListService
) {
super();
}
@ -77,6 +79,12 @@ export class WorkspaceRepositoryService extends Service {
};
};
openByWorkspaceId = (workspaceId: string) => {
const workspaceMetadata =
this.workspacesListService.list.workspace$(workspaceId).value;
return workspaceMetadata && this.open({ metadata: workspaceMetadata });
};
instantiate(
openOptions: WorkspaceOpenOptions,
customProvider?: WorkspaceEngineProvider

View File

@ -41,6 +41,10 @@ export class WorkspacesService extends Service {
return this.workspaceRepo.open;
}
get openByWorkspaceId() {
return this.workspaceRepo.openByWorkspaceId;
}
get create() {
return this.workspaceFactory.create;
}

View File

@ -137,12 +137,9 @@ events?.applicationMenu.onNewPageAction(() => {
.get(GlobalContextService)
.globalContext.workspaceId.get();
const workspacesService = frameworkProvider.get(WorkspacesService);
const workspaceMetadata = currentWorkspaceId
? workspacesService.list.workspace$(currentWorkspaceId).value
const workspaceRef = currentWorkspaceId
? workspacesService.openByWorkspaceId(currentWorkspaceId)
: null;
const workspaceRef =
workspaceMetadata &&
workspacesService.open({ metadata: workspaceMetadata });
if (!workspaceRef) {
return;
}

View File

@ -44,4 +44,4 @@ SPEC CHECKSUMS:
PODFILE CHECKSUM: 1b0d3fe81862c0e9ce712ddd0c5a0accd0097698
COCOAPODS: 1.16.2
COCOAPODS: 1.15.2

View File

@ -9,7 +9,7 @@ const config: CapacitorConfig = {
path: '.',
},
server: {
// url: 'http://localhost:8080',
url: 'http://localhost:8080',
},
plugins: {
CapacitorCookies: {

View File

@ -19,13 +19,22 @@ import {
configureBrowserWorkspaceFlavours,
configureIndexedDBWorkspaceEngineStorageProvider,
} from '@affine/core/modules/workspace-engine';
import {
docLinkBaseURLMiddleware,
MarkdownAdapter,
titleMiddleware,
} from '@blocksuite/affine/blocks';
import { Job } from '@blocksuite/affine/store';
import { App as CapacitorApp } from '@capacitor/app';
import { Browser } from '@capacitor/browser';
import {
DocsService,
Framework,
FrameworkRoot,
getCurrentStore,
GlobalContextService,
LifecycleService,
WorkspacesService,
} from '@toeverything/infra';
import { Suspense } from 'react';
import { RouterProvider } from 'react-router-dom';
@ -86,8 +95,55 @@ framework.impl(AIButtonProvider, {
return Intelligents.dismissIntelligentsButton();
},
});
const frameworkProvider = framework.provider();
(window as any).getCurrentDocContentInMarkdown = async () => {
const globalContextService = frameworkProvider.get(GlobalContextService);
const currentWorkspaceId =
globalContextService.globalContext.workspaceId.get();
const currentDocId = globalContextService.globalContext.docId.get();
const workspacesService = frameworkProvider.get(WorkspacesService);
const workspaceRef = currentWorkspaceId
? workspacesService.openByWorkspaceId(currentWorkspaceId)
: null;
if (!workspaceRef) {
return;
}
const { workspace, dispose: disposeWorkspace } = workspaceRef;
const docsService = workspace.scope.get(DocsService);
const docRef = currentDocId ? docsService.open(currentDocId) : null;
if (!docRef) {
return;
}
const { doc, release: disposeDoc } = docRef;
try {
const blockSuiteDoc = doc.blockSuiteDoc;
const job = new Job({
collection: blockSuiteDoc.collection,
middlewares: [docLinkBaseURLMiddleware, titleMiddleware],
});
const snapshot = await job.docToSnapshot(blockSuiteDoc);
const adapter = new MarkdownAdapter(job);
if (!snapshot) {
return;
}
const markdownResult = await adapter.fromDocSnapshot({
snapshot,
assets: job.assetsManager,
});
return markdownResult.file;
} finally {
disposeDoc();
disposeWorkspace();
}
};
// setup application lifecycle events, and emit application start event
window.addEventListener('focus', () => {
frameworkProvider.get(LifecycleService).applicationFocus();