chore: clean up redundant ai preset utils (#8275)
This commit is contained in:
@@ -1,75 +0,0 @@
|
|||||||
import type { EditorHost } from '@blocksuite/block-std';
|
|
||||||
import { MarkdownAdapter, titleMiddleware } from '@blocksuite/blocks';
|
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
|
||||||
import { type BlockModel, Job, type Slice } from '@blocksuite/store';
|
|
||||||
|
|
||||||
export async function getMarkdownFromSlice(host: EditorHost, slice: Slice) {
|
|
||||||
const job = new Job({
|
|
||||||
collection: host.std.doc.collection,
|
|
||||||
middlewares: [titleMiddleware],
|
|
||||||
});
|
|
||||||
const markdownAdapter = new MarkdownAdapter(job);
|
|
||||||
const markdown = await markdownAdapter.fromSlice(slice);
|
|
||||||
if (!markdown) {
|
|
||||||
return '';
|
|
||||||
}
|
|
||||||
|
|
||||||
return markdown.file;
|
|
||||||
}
|
|
||||||
export const markdownToSnapshot = async (
|
|
||||||
markdown: string,
|
|
||||||
host: EditorHost
|
|
||||||
) => {
|
|
||||||
const job = new Job({ collection: host.std.doc.collection });
|
|
||||||
const markdownAdapter = new MarkdownAdapter(job);
|
|
||||||
const { blockVersions, workspaceVersion, pageVersion } =
|
|
||||||
host.std.doc.collection.meta;
|
|
||||||
if (!blockVersions || !workspaceVersion || !pageVersion)
|
|
||||||
throw new Error(
|
|
||||||
'Need blockVersions, workspaceVersion, pageVersion meta information to get slice'
|
|
||||||
);
|
|
||||||
|
|
||||||
const payload = {
|
|
||||||
file: markdown,
|
|
||||||
assets: job.assetsManager,
|
|
||||||
blockVersions,
|
|
||||||
pageVersion,
|
|
||||||
workspaceVersion,
|
|
||||||
workspaceId: host.std.doc.collection.id,
|
|
||||||
pageId: host.std.doc.id,
|
|
||||||
};
|
|
||||||
|
|
||||||
const snapshot = await markdownAdapter.toSliceSnapshot(payload);
|
|
||||||
assertExists(snapshot, 'import markdown failed, expected to get a snapshot');
|
|
||||||
|
|
||||||
return {
|
|
||||||
snapshot,
|
|
||||||
job,
|
|
||||||
};
|
|
||||||
};
|
|
||||||
export async function insertFromMarkdown(
|
|
||||||
host: EditorHost,
|
|
||||||
markdown: string,
|
|
||||||
parent?: string,
|
|
||||||
index?: number
|
|
||||||
) {
|
|
||||||
const { snapshot, job } = await markdownToSnapshot(markdown, host);
|
|
||||||
|
|
||||||
const snapshots = snapshot.content[0].children;
|
|
||||||
|
|
||||||
const models: BlockModel[] = [];
|
|
||||||
for (let i = 0; i < snapshots.length; i++) {
|
|
||||||
const blockSnapshot = snapshots[i];
|
|
||||||
const model = await job.snapshotToBlock(
|
|
||||||
blockSnapshot,
|
|
||||||
host.std.doc,
|
|
||||||
parent,
|
|
||||||
(index ?? 0) + i
|
|
||||||
);
|
|
||||||
if (model) {
|
|
||||||
models.push(model);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return models;
|
|
||||||
}
|
|
||||||
@@ -1,120 +0,0 @@
|
|||||||
import type { EditorHost } from '@blocksuite/block-std';
|
|
||||||
import {
|
|
||||||
BlocksUtils,
|
|
||||||
EdgelessRootService,
|
|
||||||
type FrameBlockModel,
|
|
||||||
type ImageBlockModel,
|
|
||||||
type SurfaceBlockComponent,
|
|
||||||
} from '@blocksuite/blocks';
|
|
||||||
import { assertExists } from '@blocksuite/global/utils';
|
|
||||||
import { Slice } from '@blocksuite/store';
|
|
||||||
|
|
||||||
import { getMarkdownFromSlice } from './markdown-utils';
|
|
||||||
|
|
||||||
export const getRootService = (host: EditorHost) => {
|
|
||||||
return host.std.getService('affine:page');
|
|
||||||
};
|
|
||||||
|
|
||||||
export function getEdgelessRootFromEditor(editor: EditorHost) {
|
|
||||||
const edgelessRoot = editor.getElementsByTagName('affine-edgeless-root')[0];
|
|
||||||
if (!edgelessRoot) {
|
|
||||||
alert('Please switch to edgeless mode');
|
|
||||||
throw new Error('Please open switch to edgeless mode');
|
|
||||||
}
|
|
||||||
return edgelessRoot;
|
|
||||||
}
|
|
||||||
export function getEdgelessService(editor: EditorHost) {
|
|
||||||
const rootService = editor.std.getService('affine:page');
|
|
||||||
if (rootService instanceof EdgelessRootService) {
|
|
||||||
return rootService;
|
|
||||||
}
|
|
||||||
alert('Please switch to edgeless mode');
|
|
||||||
throw new Error('Please open switch to edgeless mode');
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function selectedToCanvas(editor: EditorHost) {
|
|
||||||
const edgelessRoot = getEdgelessRootFromEditor(editor);
|
|
||||||
const { notes, frames, shapes, images } = BlocksUtils.splitElements(
|
|
||||||
edgelessRoot.service.selection.selectedElements
|
|
||||||
);
|
|
||||||
if (notes.length + frames.length + images.length + shapes.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const canvas = await edgelessRoot.clipboardController.toCanvas(
|
|
||||||
[...notes, ...frames, ...images],
|
|
||||||
shapes
|
|
||||||
);
|
|
||||||
if (!canvas) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return canvas;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function frameToCanvas(
|
|
||||||
frame: FrameBlockModel,
|
|
||||||
editor: EditorHost
|
|
||||||
) {
|
|
||||||
const edgelessRoot = getEdgelessRootFromEditor(editor);
|
|
||||||
const { notes, frames, shapes, images } = BlocksUtils.splitElements(
|
|
||||||
edgelessRoot.service.frame.getElementsInFrameBound(frame, true)
|
|
||||||
);
|
|
||||||
if (notes.length + frames.length + images.length + shapes.length === 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const canvas = await edgelessRoot.clipboardController.toCanvas(
|
|
||||||
[...notes, ...frames, ...images],
|
|
||||||
shapes
|
|
||||||
);
|
|
||||||
if (!canvas) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
return canvas;
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function selectedToPng(editor: EditorHost) {
|
|
||||||
return (await selectedToCanvas(editor))?.toDataURL('image/png');
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function getSelectedTextContent(editorHost: EditorHost) {
|
|
||||||
const slice = Slice.fromModels(
|
|
||||||
editorHost.std.doc,
|
|
||||||
getRootService(editorHost)?.selectedModels ?? []
|
|
||||||
);
|
|
||||||
return getMarkdownFromSlice(editorHost, slice);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const stopPropagation = (e: Event) => {
|
|
||||||
e.stopPropagation();
|
|
||||||
};
|
|
||||||
|
|
||||||
export function getSurfaceElementFromEditor(editor: EditorHost) {
|
|
||||||
const { doc } = editor;
|
|
||||||
const surfaceModel = doc.getBlockByFlavour('affine:surface')[0];
|
|
||||||
assertExists(surfaceModel);
|
|
||||||
|
|
||||||
const surfaceId = surfaceModel.id;
|
|
||||||
const surfaceElement = editor.querySelector(
|
|
||||||
`affine-surface[data-block-id="${surfaceId}"]`
|
|
||||||
) as SurfaceBlockComponent;
|
|
||||||
assertExists(surfaceElement);
|
|
||||||
|
|
||||||
return surfaceElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getFirstImageInFrame = (
|
|
||||||
frame: FrameBlockModel,
|
|
||||||
editor: EditorHost
|
|
||||||
) => {
|
|
||||||
const edgelessRoot = getEdgelessRootFromEditor(editor);
|
|
||||||
const elements = edgelessRoot.service.frame.getElementsInFrameBound(
|
|
||||||
frame,
|
|
||||||
false
|
|
||||||
);
|
|
||||||
const image = elements.find(ele => {
|
|
||||||
if (!BlocksUtils.isCanvasElement(ele)) {
|
|
||||||
return ele.flavour === 'affine:image';
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}) as ImageBlockModel | undefined;
|
|
||||||
return image?.id;
|
|
||||||
};
|
|
||||||
@@ -28,8 +28,6 @@ import { html, type TemplateResult } from 'lit';
|
|||||||
import { styleMap } from 'lit/directives/style-map.js';
|
import { styleMap } from 'lit/directives/style-map.js';
|
||||||
|
|
||||||
import { AIPenIcon, ChatWithAIIcon } from '../_common/icons';
|
import { AIPenIcon, ChatWithAIIcon } from '../_common/icons';
|
||||||
import { insertFromMarkdown } from '../_common/markdown-utils';
|
|
||||||
import { getSurfaceElementFromEditor } from '../_common/selection-utils';
|
|
||||||
import { getAIPanel } from '../ai-panel';
|
import { getAIPanel } from '../ai-panel';
|
||||||
import { AIProvider } from '../provider';
|
import { AIProvider } from '../provider';
|
||||||
import { reportResponse } from '../utils/action-reporter';
|
import { reportResponse } from '../utils/action-reporter';
|
||||||
@@ -40,10 +38,12 @@ import {
|
|||||||
} from '../utils/edgeless';
|
} from '../utils/edgeless';
|
||||||
import { preprocessHtml } from '../utils/html';
|
import { preprocessHtml } from '../utils/html';
|
||||||
import { fetchImageToFile } from '../utils/image';
|
import { fetchImageToFile } from '../utils/image';
|
||||||
|
import { insertFromMarkdown } from '../utils/markdown-utils';
|
||||||
import {
|
import {
|
||||||
getCopilotSelectedElems,
|
getCopilotSelectedElems,
|
||||||
getEdgelessRootFromEditor,
|
getEdgelessRootFromEditor,
|
||||||
getEdgelessService,
|
getEdgelessService,
|
||||||
|
getSurfaceElementFromEditor,
|
||||||
} from '../utils/selection-utils';
|
} from '../utils/selection-utils';
|
||||||
import { EXCLUDING_INSERT_ACTIONS, generatingStages } from './consts';
|
import { EXCLUDING_INSERT_ACTIONS, generatingStages } from './consts';
|
||||||
import type { CtxRecord } from './types';
|
import type { CtxRecord } from './types';
|
||||||
@@ -192,7 +192,7 @@ function insertBelow(
|
|||||||
parentId: string,
|
parentId: string,
|
||||||
index = 0
|
index = 0
|
||||||
) {
|
) {
|
||||||
insertFromMarkdown(host, markdown, parentId, index)
|
insertFromMarkdown(host, markdown, host.doc, parentId, index)
|
||||||
.then(() => {
|
.then(() => {
|
||||||
const service = getService(host);
|
const service = getService(host);
|
||||||
|
|
||||||
|
|||||||
@@ -2,8 +2,8 @@ import type { EditorHost } from '@blocksuite/block-std';
|
|||||||
import type { EdgelessRootService } from '@blocksuite/blocks';
|
import type { EdgelessRootService } from '@blocksuite/blocks';
|
||||||
import type { BlockSnapshot } from '@blocksuite/store';
|
import type { BlockSnapshot } from '@blocksuite/store';
|
||||||
|
|
||||||
import { markdownToSnapshot } from '../_common/markdown-utils';
|
import { markdownToSnapshot } from '../utils/markdown-utils';
|
||||||
import { getSurfaceElementFromEditor } from '../_common/selection-utils';
|
import { getSurfaceElementFromEditor } from '../utils/selection-utils';
|
||||||
import {
|
import {
|
||||||
basicTheme,
|
basicTheme,
|
||||||
type PPTDoc,
|
type PPTDoc,
|
||||||
|
|||||||
Reference in New Issue
Block a user