feat: add @affine/sdk (#3536)

This commit is contained in:
Alex Yang
2023-08-02 21:47:05 -07:00
committed by GitHub
parent d3c719d89a
commit ea34d66e14
39 changed files with 256 additions and 93 deletions
+1
View File
@@ -36,6 +36,7 @@
}
},
"dependencies": {
"@affine/sdk": "workspace:*",
"@blocksuite/global": "0.0.0-20230802200139-381599c0-nightly",
"@blocksuite/store": "0.0.0-20230802200139-381599c0-nightly",
"jotai": "^2.2.2",
+1 -2
View File
@@ -1,10 +1,9 @@
import type { CallbackMap, ExpectedLayout } from '@affine/sdk/entry';
import { assertExists } from '@blocksuite/global/utils';
import type { Page, Workspace } from '@blocksuite/store';
import { atom, createStore } from 'jotai/vanilla';
import { getWorkspace, waitForWorkspace } from './__internal__/workspace.js';
import type { CallbackMap } from './entry.js';
import type { ExpectedLayout } from './type.js';
// global store
export const rootStore = createStore();
-25
View File
@@ -1,25 +0,0 @@
import type { getCurrentBlockRange } from '@blocksuite/blocks';
import type { EditorContainer } from '@blocksuite/editor';
import type { Page } from '@blocksuite/store';
import type { FC } from 'react';
export type Part = 'headerItem' | 'editor' | 'window' | 'setting' | 'formatBar';
export type CallbackMap = {
headerItem: (root: HTMLElement) => () => void;
window: (root: HTMLElement) => () => void;
editor: (root: HTMLElement, editor: EditorContainer) => () => void;
setting: (root: HTMLElement) => () => void;
formatBar: (
root: HTMLElement,
page: Page,
getBlockRange: () => ReturnType<typeof getCurrentBlockRange>
) => () => void;
};
export interface PluginContext {
register: <T extends Part>(part: T, callback: CallbackMap[T]) => void;
utils: {
PluginProvider: FC;
};
}
+1 -20
View File
@@ -1,3 +1,4 @@
import type { ExpectedLayout } from '@affine/sdk/entry';
import type { WritableAtom } from 'jotai';
import { z } from 'zod';
@@ -29,26 +30,6 @@ export const packageJsonOutputSchema = z.object({
}),
});
export type LayoutDirection = 'horizontal' | 'vertical';
export type LayoutNode = LayoutParentNode | string;
export type LayoutParentNode = {
direction: LayoutDirection;
splitPercentage: number; // 0 - 100
first: LayoutNode;
second: LayoutNode;
};
export type ExpectedLayout =
| {
direction: LayoutDirection;
// the first element is always the editor
first: 'editor';
second: LayoutNode;
// the percentage should be greater than 70
splitPercentage: number;
}
| 'editor';
type SetStateAction<Value> = Value | ((prev: Value) => Value);
export type ContentLayoutAtom = WritableAtom<
-1
View File
@@ -11,7 +11,6 @@ export default defineConfig({
minify: false,
lib: {
entry: {
entry: resolve(root, 'src/entry.ts'),
type: resolve(root, 'src/type.ts'),
atom: resolve(root, 'src/atom.ts'),
'__internal__/workspace': resolve(
+35
View File
@@ -0,0 +1,35 @@
{
"name": "@affine/sdk",
"version": "0.0.1",
"type": "module",
"scripts": {
"build": "vite build",
"dev": "vite build --watch"
},
"exports": {
"./entry": {
"types": "./dist/src/entry.d.ts",
"import": "./dist/entry.js",
"require": "./dist/entry.cjs"
},
"./server": {
"types": "./dist/src/server.d.ts",
"import": "./dist/server.js",
"require": "./dist/server.cjs"
}
},
"files": [
"dist"
],
"dependencies": {
"@blocksuite/blocks": "0.0.0-20230802200139-381599c0-nightly",
"@blocksuite/global": "0.0.0-20230802200139-381599c0-nightly",
"@blocksuite/store": "0.0.0-20230802200139-381599c0-nightly",
"jotai": "^2.2.2",
"zod": "^3.21.4"
},
"devDependencies": {
"vite": "^4.4.7",
"vite-plugin-dts": "3.3.1"
}
}
+21
View File
@@ -0,0 +1,21 @@
{
"name": "sdk",
"$schema": "../../node_modules/nx/schemas/project-schema.json",
"projectType": "library",
"sourceRoot": "packages/sdk/src",
"targets": {
"build": {
"executor": "@nx/vite:build",
"options": {
"outputPath": "packages/sdk/dist"
}
},
"serve": {
"executor": "@nx/vite:build",
"options": {
"outputPath": "packages/sdk/dist",
"watch": true
}
}
}
}
+52
View File
@@ -0,0 +1,52 @@
import type { getCurrentBlockRange } from '@blocksuite/blocks';
import type { EditorContainer } from '@blocksuite/editor';
import type { Page } from '@blocksuite/store';
import type { Workspace } from '@blocksuite/store';
import type { Atom, getDefaultStore, PrimitiveAtom } from 'jotai/vanilla';
import type { FC } from 'react';
export type Part = 'headerItem' | 'editor' | 'window' | 'setting' | 'formatBar';
export type CallbackMap = {
headerItem: (root: HTMLElement) => () => void;
window: (root: HTMLElement) => () => void;
editor: (root: HTMLElement, editor: EditorContainer) => () => void;
setting: (root: HTMLElement) => () => void;
formatBar: (
root: HTMLElement,
page: Page,
getBlockRange: () => ReturnType<typeof getCurrentBlockRange>
) => () => void;
};
export interface PluginContext {
register: <T extends Part>(part: T, callback: CallbackMap[T]) => void;
utils: {
PluginProvider: FC;
};
}
export type LayoutDirection = 'horizontal' | 'vertical';
export type LayoutNode = LayoutParentNode | string;
export type LayoutParentNode = {
direction: LayoutDirection;
splitPercentage: number; // 0 - 100
first: LayoutNode;
second: LayoutNode;
};
export type ExpectedLayout =
| {
direction: LayoutDirection;
// the first element is always the editor
first: 'editor';
second: LayoutNode;
// the percentage should be greater than 70
splitPercentage: number;
}
| 'editor';
export declare const contentLayoutAtom: PrimitiveAtom<ExpectedLayout>;
export declare const currentPageAtom: Atom<Promise<Page>>;
export declare const currentWorkspaceAtom: Atom<Promise<Workspace>>;
export declare const rootStore: ReturnType<typeof getDefaultStore>;
+15
View File
@@ -0,0 +1,15 @@
{
"extends": "../../tsconfig.json",
"include": ["./src"],
"compilerOptions": {
"composite": true,
"noEmit": false,
"moduleResolution": "Node16",
"outDir": "lib"
},
"references": [
{
"path": "./tsconfig.node.json"
}
]
}
+12
View File
@@ -0,0 +1,12 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true,
"outDir": "lib",
"noEmit": false
},
"include": ["vite.config.ts"]
}
+23
View File
@@ -0,0 +1,23 @@
import { resolve } from 'node:path';
import { fileURLToPath } from 'url';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
const root = fileURLToPath(new URL('.', import.meta.url));
export default defineConfig({
build: {
minify: false,
lib: {
entry: {
entry: resolve(root, 'src/entry.ts'),
server: resolve(root, 'src/server.ts'),
},
},
rollupOptions: {
external: [/^jotai/, /^@blocksuite/, 'zod'],
},
},
plugins: [dts()],
});