Skip to content

Commit

Permalink
feat: add @affine/sdk (toeverything#3536)
Browse files Browse the repository at this point in the history
  • Loading branch information
himself65 authored Aug 3, 2023
1 parent d3c719d commit ea34d66
Show file tree
Hide file tree
Showing 39 changed files with 256 additions and 93 deletions.
3 changes: 2 additions & 1 deletion .commitlintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"storage",
"infra",
"plugin-infra",
"plugin-cli"
"plugin-cli",
"sdk"
]
]
}
Expand Down
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ lib
.eslintrc.js
packages/i18n/src/i18n-generated.ts
e2e-dist-*
static
web-static
public
6 changes: 5 additions & 1 deletion .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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/**/*'
Expand Down
19 changes: 5 additions & 14 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -490,7 +484,6 @@ jobs:
if: github.ref == 'refs/heads/master'
name: Build Docker
needs:
- lint
- desktop-test
- server-test
runs-on: ubuntu-latest
Expand All @@ -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:
Expand Down
19 changes: 15 additions & 4 deletions apps/core/src/bootstrap/plugins/setup.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down Expand Up @@ -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)));
};

Expand Down
3 changes: 1 addition & 2 deletions apps/core/src/components/page-detail-editor.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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';
Expand Down
14 changes: 14 additions & 0 deletions apps/electron/forge.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions apps/electron/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion apps/electron/src/worker/plugin.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 3 additions & 0 deletions apps/electron/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
{
"path": "../../packages/infra"
},
{
"path": "../../packages/sdk"
},
{
"path": "../../packages/env"
},
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 1 addition & 2 deletions packages/plugin-infra/src/atom.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand Down
25 changes: 0 additions & 25 deletions packages/plugin-infra/src/entry.ts

This file was deleted.

21 changes: 1 addition & 20 deletions packages/plugin-infra/src/type.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ExpectedLayout } from '@affine/sdk/entry';
import type { WritableAtom } from 'jotai';
import { z } from 'zod';

Expand Down Expand Up @@ -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<
Expand Down
1 change: 0 additions & 1 deletion packages/plugin-infra/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
35 changes: 35 additions & 0 deletions packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions packages/sdk/project.json
Original file line number Diff line number Diff line change
@@ -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 changes: 52 additions & 0 deletions packages/sdk/src/entry.ts
Original file line number Diff line number Diff line change
@@ -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>;
File renamed without changes.
Loading

0 comments on commit ea34d66

Please sign in to comment.