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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 256 additions and 93 deletions

View File

@ -25,7 +25,8 @@
"storage",
"infra",
"plugin-infra",
"plugin-cli"
"plugin-cli",
"sdk"
]
]
}

View File

@ -9,3 +9,6 @@ lib
.eslintrc.js
packages/i18n/src/i18n-generated.ts
e2e-dist-*
static
web-static
public

6
.github/labeler.yml vendored
View File

@ -22,8 +22,12 @@ plugin:bookmark-block:
plugin:copilot:
- 'plugins/copilot/**/*'
mod:plugin-infra:
mod:infra:
- 'packages/plugin-infra/**/*'
- 'packages/infra/**/*'
mod:sdk:
- 'packages/sdk/**/*'
mod:plugin-cli:
- 'packages/plugin-cli/**/*'

View File

@ -47,8 +47,6 @@ jobs:
electron-install: false
- name: Run i18n codegen
run: yarn i18n-codegen gen
- name: Run Type Check
run: yarn typecheck
- name: Run ESLint
run: yarn lint:eslint --max-warnings=0
- name: Run Prettier
@ -58,12 +56,8 @@ jobs:
yarn lint:prettier
- name: Run circular
run: yarn circular
- name: Upload server dist
uses: actions/upload-artifact@v3
with:
name: server-dist
path: ./apps/server/dist
if-no-files-found: error
- name: Run Type Check
run: yarn typecheck
build-docs:
name: Build Docs
@ -490,7 +484,6 @@ jobs:
if: github.ref == 'refs/heads/master'
name: Build Docker
needs:
- lint
- desktop-test
- server-test
runs-on: ubuntu-latest
@ -501,11 +494,9 @@ jobs:
with:
name: core
path: ./apps/core/dist
- name: Download server dist
uses: actions/download-artifact@v3
with:
name: server-dist
path: ./apps/server/dist
- name: Compile server code
run: tsc -b
working-directory: ./apps/server
- name: Download storage.node
uses: actions/download-artifact@v3
with:

View File

@ -1,5 +1,6 @@
import * as AFFiNEComponent from '@affine/component';
import { DebugLogger } from '@affine/debug';
import type { CallbackMap, PluginContext } from '@affine/sdk/entry';
import { FormatQuickBar } from '@blocksuite/blocks';
import * as BlockSuiteBlocksStd from '@blocksuite/blocks/std';
import * as BlockSuiteGlobalUtils from '@blocksuite/global/utils';
@ -8,16 +9,15 @@ import { DisposableGroup } from '@blocksuite/global/utils';
import * as Icons from '@blocksuite/icons';
import * as Atom from '@toeverything/plugin-infra/atom';
import {
contentLayoutAtom,
currentPageAtom,
currentWorkspaceAtom,
editorItemsAtom,
headerItemsAtom,
rootStore,
settingItemsAtom,
windowItemsAtom,
} from '@toeverything/plugin-infra/atom';
import type {
CallbackMap,
PluginContext,
} from '@toeverything/plugin-infra/entry';
import * as Jotai from 'jotai/index';
import { Provider } from 'jotai/react';
import * as JotaiUtils from 'jotai/utils';
@ -66,6 +66,17 @@ const setupRootImportsMap = () => {
'@toeverything/plugin-infra/atom',
new Map(Object.entries(Atom))
);
_rootImportsMap.set(
'@affine/sdk/entry',
new Map(
Object.entries({
rootStore: rootStore,
currentWorkspaceAtom: currentWorkspaceAtom,
currentPageAtom: currentPageAtom,
contentLayoutAtom: contentLayoutAtom,
})
)
);
_rootImportsMap.set('swr', new Map(Object.entries(SWR)));
};

View File

@ -1,6 +1,7 @@
import './page-detail-editor.css';
import { PageNotFoundError } from '@affine/env/constant';
import type { CallbackMap, LayoutNode } from '@affine/sdk//entry';
import { rootBlockHubAtom } from '@affine/workspace/atom';
import type { EditorContainer } from '@blocksuite/editor';
import { assertExists } from '@blocksuite/global/utils';
@ -13,8 +14,6 @@ import {
rootStore,
windowItemsAtom,
} from '@toeverything/plugin-infra/atom';
import type { CallbackMap } from '@toeverything/plugin-infra/entry';
import type { LayoutNode } from '@toeverything/plugin-infra/type';
import clsx from 'clsx';
import { useAtomValue, useSetAtom } from 'jotai';
import type { CSSProperties, FC, ReactElement } from 'react';

View File

@ -137,6 +137,20 @@ module.exports = {
force: true,
}
);
await rm(resolve(__dirname, './node_modules/@affine/sdk'), {
recursive: true,
force: true,
});
await cp(
resolve(__dirname, '../../packages/sdk'),
resolve(__dirname, './node_modules/@affine/sdk'),
{
recursive: true,
force: true,
}
);
},
generateAssets: async (_, platform, arch) => {
if (process.env.SKIP_GENERATE_ASSETS) {

View File

@ -27,6 +27,7 @@
"@affine/env": "workspace:*",
"@affine/maker-dmg": "workspace:*",
"@affine/native": "workspace:*",
"@affine/sdk": "workspace:*",
"@blocksuite/blocks": "0.0.0-20230802200139-381599c0-nightly",
"@blocksuite/editor": "0.0.0-20230802200139-381599c0-nightly",
"@blocksuite/lit": "0.0.0-20230802200139-381599c0-nightly",

View File

@ -1,7 +1,7 @@
import { join, resolve } from 'node:path';
import { parentPort } from 'node:worker_threads';
import type { ServerContext } from '@toeverything/plugin-infra/server';
import type { ServerContext } from '@affine/sdk/server';
import { AsyncCall } from 'async-call-rpc';
import { MessageEventChannel } from '../shared/utils';

View File

@ -25,6 +25,9 @@
{
"path": "../../packages/infra"
},
{
"path": "../../packages/sdk"
},
{
"path": "../../packages/env"
},

View File

@ -24,7 +24,7 @@
"build": "yarn nx build @affine/core",
"build:electron": "yarn nx build @affine/electron",
"build:storage": "yarn nx run-many -t build -p @affine/storage",
"build:infra": "yarn nx run-many -t build -p plugin-infra infra",
"build:infra": "yarn nx run-many -t build -p plugin-infra infra sdk",
"build:plugins": "yarn nx run-many -t build --projects=tag:plugin",
"build:storybook": "yarn nx build @affine/storybook",
"start:web-static": "yarn workspace @affine/core static-server",

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",

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();

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;
};
}

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<

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
packages/sdk/package.json Normal file
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
packages/sdk/project.json Normal file
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
packages/sdk/src/entry.ts Normal file
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>;

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"
}
]
}

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"]
}

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()],
});

View File

@ -19,8 +19,8 @@
},
"dependencies": {
"@affine/component": "workspace:*",
"@affine/sdk": "workspace:*",
"@blocksuite/icons": "^2.1.29",
"@toeverything/plugin-infra": "workspace:*",
"foxact": "^0.2.17",
"link-preview-js": "^3.0.4"
},

View File

@ -1,4 +1,4 @@
import type { PluginContext } from '@toeverything/plugin-infra/entry';
import type { PluginContext } from '@affine/sdk/entry';
import { createElement } from 'react';
import { createRoot } from 'react-dom/client';

View File

@ -1,4 +1,4 @@
import type { ServerContext } from '@toeverything/plugin-infra/server';
import type { ServerContext } from '@affine/sdk/server';
import { getLinkPreview } from 'link-preview-js';
type MetaData = {

View File

@ -8,7 +8,7 @@
},
"references": [
{
"path": "../../packages/plugin-infra"
"path": "../../packages/sdk"
},
{
"path": "../../packages/component"

View File

@ -15,7 +15,7 @@
},
"dependencies": {
"@affine/component": "workspace:*",
"@toeverything/plugin-infra": "workspace:*",
"@affine/sdk": "workspace:*",
"idb": "^7.1.1",
"langchain": "^0.0.118",
"marked": "^5.1.1",

View File

@ -1,4 +1,4 @@
import type { PluginContext } from '@toeverything/plugin-infra/entry';
import type { PluginContext } from '@affine/sdk/entry';
import { createElement } from 'react';
import { createRoot } from 'react-dom/client';

View File

@ -10,7 +10,7 @@
"path": "../../packages/component"
},
{
"path": "../../packages/plugin-infra"
"path": "../../packages/sdk"
},
{
"path": "../../packages/env"

View File

@ -16,6 +16,7 @@
},
"dependencies": {
"@affine/component": "workspace:*",
"@affine/sdk": "workspace:*",
"@blocksuite/icons": "^2.1.29",
"@toeverything/plugin-infra": "workspace:*"
},

View File

@ -1,8 +1,5 @@
import {
currentWorkspaceIdAtom,
rootStore,
} from '@toeverything/plugin-infra/atom';
import type { PluginContext } from '@toeverything/plugin-infra/entry';
import type { PluginContext } from '@affine/sdk/entry';
import { currentWorkspaceAtom, rootStore } from '@affine/sdk/entry';
import { createElement } from 'react';
import { lazy } from 'react';
import { createRoot } from 'react-dom/client';
@ -14,7 +11,7 @@ const HeaderItem = lazy(() =>
export const entry = (context: PluginContext) => {
console.log('register');
console.log('hello, world!');
console.log(rootStore.get(currentWorkspaceIdAtom));
console.log(rootStore.get(currentWorkspaceAtom));
context.register('headerItem', div => {
const root = createRoot(div);
root.render(createElement(HeaderItem));

View File

@ -8,7 +8,7 @@
},
"references": [
{
"path": "../../packages/plugin-infra"
"path": "../../packages/sdk"
},
{
"path": "../../packages/component"

View File

@ -15,8 +15,8 @@
},
"dependencies": {
"@affine/component": "workspace:*",
"@affine/sdk": "workspace:*",
"@blocksuite/icons": "^2.1.29",
"@toeverything/plugin-infra": "workspace:*",
"@toeverything/theme": "^0.7.9",
"clsx": "^2.0.0",
"foxact": "^0.2.17",

View File

@ -1,4 +1,4 @@
import type { PluginContext } from '@toeverything/plugin-infra/entry';
import type { PluginContext } from '@affine/sdk/entry';
import { createElement } from 'react';
import { createRoot } from 'react-dom/client';

View File

@ -8,7 +8,7 @@
},
"references": [
{
"path": "../../packages/plugin-infra"
"path": "../../packages/sdk"
},
{
"path": "../../packages/component"

View File

@ -4,6 +4,7 @@ packages=(
"y-indexeddb"
"infra"
"plugin-infra"
"sdk"
)
for package in "${packages[@]}"; do

View File

@ -77,6 +77,7 @@
"@toeverything/hooks/*": ["./packages/hooks/src/*"],
"@toeverything/infra/*": ["./packages/infra/src/*"],
"@toeverything/plugin-infra/*": ["./packages/plugin-infra/src/*"],
"@affine/sdk/*": ["./packages/sdk/src/*"],
"@affine/native": ["./packages/native/index.d.ts"],
"@affine/native/*": ["./packages/native/*"],
"@affine/storage": ["./packages/storage/index.d.ts"],
@ -104,6 +105,9 @@
{
"path": "./packages/infra"
},
{
"path": "./packages/plugin-infra"
},
{
"path": "./packages/graphql"
},
@ -112,7 +116,7 @@
},
// Plugins
{
"path": "./packages/plugin-infra"
"path": "./packages/sdk"
},
{
"path": "./plugins/bookmark"
@ -123,6 +127,9 @@
{
"path": "./plugins/hello-world"
},
{
"path": "./plugins/image-preview"
},
// Packages
{
"path": "./packages/cli"

View File

@ -95,8 +95,8 @@ __metadata:
dependencies:
"@affine/component": "workspace:*"
"@affine/plugin-cli": "workspace:*"
"@affine/sdk": "workspace:*"
"@blocksuite/icons": ^2.1.29
"@toeverything/plugin-infra": "workspace:*"
foxact: ^0.2.17
link-preview-js: ^3.0.4
languageName: unknown
@ -186,7 +186,7 @@ __metadata:
dependencies:
"@affine/component": "workspace:*"
"@affine/plugin-cli": "workspace:*"
"@toeverything/plugin-infra": "workspace:*"
"@affine/sdk": "workspace:*"
"@types/marked": ^5.0.1
idb: ^7.1.1
jotai: ^2.2.2
@ -321,6 +321,7 @@ __metadata:
"@affine/env": "workspace:*"
"@affine/maker-dmg": "workspace:*"
"@affine/native": "workspace:*"
"@affine/sdk": "workspace:*"
"@blocksuite/blocks": 0.0.0-20230802200139-381599c0-nightly
"@blocksuite/editor": 0.0.0-20230802200139-381599c0-nightly
"@blocksuite/lit": 0.0.0-20230802200139-381599c0-nightly
@ -401,6 +402,7 @@ __metadata:
dependencies:
"@affine/component": "workspace:*"
"@affine/plugin-cli": "workspace:*"
"@affine/sdk": "workspace:*"
"@blocksuite/icons": ^2.1.29
"@toeverything/plugin-infra": "workspace:*"
languageName: unknown
@ -426,8 +428,8 @@ __metadata:
dependencies:
"@affine/component": "workspace:*"
"@affine/plugin-cli": "workspace:*"
"@affine/sdk": "workspace:*"
"@blocksuite/icons": ^2.1.29
"@toeverything/plugin-infra": "workspace:*"
"@toeverything/theme": ^0.7.9
clsx: ^2.0.0
foxact: ^0.2.17
@ -565,6 +567,20 @@ __metadata:
languageName: unknown
linkType: soft
"@affine/sdk@workspace:*, @affine/sdk@workspace:packages/sdk":
version: 0.0.0-use.local
resolution: "@affine/sdk@workspace:packages/sdk"
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
vite: ^4.4.7
vite-plugin-dts: 3.3.1
zod: ^3.21.4
languageName: unknown
linkType: soft
"@affine/server@workspace:apps/server":
version: 0.0.0-use.local
resolution: "@affine/server@workspace:apps/server"
@ -11396,6 +11412,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@toeverything/plugin-infra@workspace:packages/plugin-infra"
dependencies:
"@affine/sdk": "workspace:*"
"@blocksuite/blocks": 0.0.0-20230802200139-381599c0-nightly
"@blocksuite/editor": 0.0.0-20230802200139-381599c0-nightly
"@blocksuite/global": 0.0.0-20230802200139-381599c0-nightly