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, [ .service(WorkspaceRepositoryService, [
[WorkspaceFlavourProvider], [WorkspaceFlavourProvider],
WorkspaceProfileService, WorkspaceProfileService,
WorkspaceListService,
]) ])
.scope(WorkspaceScope) .scope(WorkspaceScope)
.service(WorkspaceService) .service(WorkspaceService)

View File

@ -11,6 +11,7 @@ import type {
WorkspaceFlavourProvider, WorkspaceFlavourProvider,
} from '../providers/flavour'; } from '../providers/flavour';
import { WorkspaceScope } from '../scopes/workspace'; import { WorkspaceScope } from '../scopes/workspace';
import type { WorkspaceListService } from './list';
import type { WorkspaceProfileService } from './profile'; import type { WorkspaceProfileService } from './profile';
import { WorkspaceService } from './workspace'; import { WorkspaceService } from './workspace';
@ -19,7 +20,8 @@ const logger = new DebugLogger('affine:workspace-repository');
export class WorkspaceRepositoryService extends Service { export class WorkspaceRepositoryService extends Service {
constructor( constructor(
private readonly providers: WorkspaceFlavourProvider[], private readonly providers: WorkspaceFlavourProvider[],
private readonly profileRepo: WorkspaceProfileService private readonly profileRepo: WorkspaceProfileService,
private readonly workspacesListService: WorkspaceListService
) { ) {
super(); 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( instantiate(
openOptions: WorkspaceOpenOptions, openOptions: WorkspaceOpenOptions,
customProvider?: WorkspaceEngineProvider customProvider?: WorkspaceEngineProvider

View File

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

View File

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

View File

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

View File

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

View File

@ -19,13 +19,22 @@ import {
configureBrowserWorkspaceFlavours, configureBrowserWorkspaceFlavours,
configureIndexedDBWorkspaceEngineStorageProvider, configureIndexedDBWorkspaceEngineStorageProvider,
} from '@affine/core/modules/workspace-engine'; } 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 { App as CapacitorApp } from '@capacitor/app';
import { Browser } from '@capacitor/browser'; import { Browser } from '@capacitor/browser';
import { import {
DocsService,
Framework, Framework,
FrameworkRoot, FrameworkRoot,
getCurrentStore, getCurrentStore,
GlobalContextService,
LifecycleService, LifecycleService,
WorkspacesService,
} from '@toeverything/infra'; } from '@toeverything/infra';
import { Suspense } from 'react'; import { Suspense } from 'react';
import { RouterProvider } from 'react-router-dom'; import { RouterProvider } from 'react-router-dom';
@ -86,8 +95,55 @@ framework.impl(AIButtonProvider, {
return Intelligents.dismissIntelligentsButton(); return Intelligents.dismissIntelligentsButton();
}, },
}); });
const frameworkProvider = framework.provider(); 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 // setup application lifecycle events, and emit application start event
window.addEventListener('focus', () => { window.addEventListener('focus', () => {
frameworkProvider.get(LifecycleService).applicationFocus(); frameworkProvider.get(LifecycleService).applicationFocus();