mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 12:28:42 +00:00
chore: merge blocksuite source code (#9213)
This commit is contained in:
43
blocksuite/framework/block-std/package.json
Normal file
43
blocksuite/framework/block-std/package.json
Normal file
@@ -0,0 +1,43 @@
|
||||
{
|
||||
"name": "@blocksuite/block-std",
|
||||
"description": "Std for blocksuite blocks",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"test:unit": "nx vite:test --run",
|
||||
"test:unit:coverage": "nx vite:test --run --coverage",
|
||||
"test:unit:ui": "nx vite:test --ui",
|
||||
"test": "yarn test:unit"
|
||||
},
|
||||
"sideEffects": false,
|
||||
"keywords": [],
|
||||
"author": "toeverything",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@blocksuite/global": "workspace:*",
|
||||
"@blocksuite/inline": "workspace:*",
|
||||
"@blocksuite/store": "workspace:*",
|
||||
"@lit/context": "^1.1.2",
|
||||
"@preact/signals-core": "^1.8.0",
|
||||
"@types/hast": "^3.0.4",
|
||||
"fractional-indexing": "^3.2.0",
|
||||
"lib0": "^0.2.97",
|
||||
"lit": "^3.2.0",
|
||||
"lz-string": "^1.5.0",
|
||||
"rehype-parse": "^9.0.0",
|
||||
"unified": "^11.0.5",
|
||||
"w3c-keyname": "^2.2.8",
|
||||
"zod": "^3.23.8"
|
||||
},
|
||||
"exports": {
|
||||
".": "./src/index.ts",
|
||||
"./gfx": "./src/gfx/index.ts",
|
||||
"./effects": "./src/effects.ts"
|
||||
},
|
||||
"files": [
|
||||
"src",
|
||||
"dist",
|
||||
"!src/__tests__",
|
||||
"!dist/__tests__"
|
||||
]
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 2.9 KiB |
@@ -0,0 +1,506 @@
|
||||
import { beforeEach, describe, expect, test, vi } from 'vitest';
|
||||
|
||||
import type { Command } from '../command/index.js';
|
||||
import { CommandManager } from '../command/index.js';
|
||||
|
||||
type Command1 = Command<
|
||||
never,
|
||||
'commandData1',
|
||||
{
|
||||
command1Option?: string;
|
||||
}
|
||||
>;
|
||||
|
||||
type Command2 = Command<'commandData1', 'commandData2'>;
|
||||
|
||||
type Command3 = Command<'commandData1' | 'commandData2', 'commandData3'>;
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext {
|
||||
commandData1?: string;
|
||||
commandData2?: string;
|
||||
commandData3?: string;
|
||||
}
|
||||
|
||||
interface Commands {
|
||||
command1: Command1;
|
||||
command2: Command2;
|
||||
command3: Command3;
|
||||
command4: Command;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
describe('CommandManager', () => {
|
||||
let std: BlockSuite.Std;
|
||||
let commandManager: CommandManager;
|
||||
|
||||
beforeEach(() => {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
std = {};
|
||||
commandManager = new CommandManager(std);
|
||||
});
|
||||
|
||||
test('can add and execute a command', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) => next());
|
||||
const command2: Command = vi.fn((_ctx, _next) => {});
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
|
||||
const [success1] = commandManager.chain().command1().run();
|
||||
const [success2] = commandManager.chain().command2().run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(success1).toBeTruthy();
|
||||
expect(success2).toBeFalsy();
|
||||
});
|
||||
|
||||
test('can chain multiple commands', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) => next());
|
||||
const command2: Command = vi.fn((_ctx, next) => next());
|
||||
const command3: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.command1()
|
||||
.command2()
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
});
|
||||
|
||||
test('skip commands if there is a command failed before them (`next` not executed)', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) => next());
|
||||
const command2: Command = vi.fn((_ctx, _next) => {});
|
||||
const command3: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.command1()
|
||||
.command2()
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).not.toHaveBeenCalled();
|
||||
expect(success).toBeFalsy();
|
||||
});
|
||||
|
||||
test('can handle command failure', () => {
|
||||
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
|
||||
const command1: Command = vi.fn((_ctx, next) => next());
|
||||
const command2: Command = vi.fn((_ctx, _next) => {
|
||||
throw new Error('command2 failed');
|
||||
});
|
||||
const command3: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.command1()
|
||||
.command2()
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).not.toHaveBeenCalled();
|
||||
expect(success).toBeFalsy();
|
||||
expect(errorSpy).toHaveBeenCalledWith(new Error('command2 failed'));
|
||||
});
|
||||
|
||||
test('can pass data to command when calling a command', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.command1({ command1Option: 'test' })
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ command1Option: 'test' }),
|
||||
expect.any(Function)
|
||||
);
|
||||
expect(success).toBeTruthy();
|
||||
});
|
||||
|
||||
test('can add data to the command chain with `with` method', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
|
||||
const [success, ctx] = commandManager
|
||||
.chain()
|
||||
.with({ commandData1: 'test' })
|
||||
.command1()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ commandData1: 'test' }),
|
||||
expect.any(Function)
|
||||
);
|
||||
expect(success).toBeTruthy();
|
||||
expect(ctx.commandData1).toBe('test');
|
||||
});
|
||||
|
||||
test('passes and updates context across commands', () => {
|
||||
const command1: Command<'std', 'commandData1'> = vi.fn((_ctx, next) =>
|
||||
next({ commandData1: '123' })
|
||||
);
|
||||
const command2: Command<'commandData1'> = vi.fn((ctx, next) => {
|
||||
expect(ctx.commandData1).toBe('123');
|
||||
next({ commandData1: '456' });
|
||||
});
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
|
||||
const [success, ctx] = commandManager.chain().command1().command2().run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
expect(ctx.commandData1).toBe('456');
|
||||
});
|
||||
|
||||
test('can execute an inline command', () => {
|
||||
const inlineCommand: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
const success = commandManager.chain().inline(inlineCommand).run();
|
||||
|
||||
expect(inlineCommand).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
});
|
||||
|
||||
test('can execute a single command with `exec`', () => {
|
||||
const command1: Command1 = vi.fn((_ctx, next) =>
|
||||
next({ commandData1: (_ctx.command1Option ?? '') + '123' })
|
||||
);
|
||||
const command2: Command2 = vi.fn((_ctx, next) =>
|
||||
next({ commandData2: 'cmd2' })
|
||||
);
|
||||
const command3: Command3 = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const result1 = commandManager.exec('command1');
|
||||
const result2 = commandManager.exec('command1', {
|
||||
command1Option: 'test',
|
||||
});
|
||||
const result3 = commandManager.exec('command2');
|
||||
const result4 = commandManager.exec('command3');
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(result1).toEqual({ commandData1: '123', success: true });
|
||||
expect(result2).toEqual({ commandData1: 'test123', success: true });
|
||||
expect(result3).toEqual({ commandData2: 'cmd2', success: true });
|
||||
expect(result4).toEqual({ success: true });
|
||||
});
|
||||
|
||||
test('should not continue with the rest of the chain if all commands in `try` fail', () => {
|
||||
const command1: Command<never, 'commandData1'> = vi.fn((_ctx, _next) => {});
|
||||
const command2: Command = vi.fn((_ctx, _next) => {});
|
||||
const command3: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.try(cmd => [cmd.command1(), cmd.command2()])
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).not.toHaveBeenCalled();
|
||||
expect(success).toBeFalsy();
|
||||
});
|
||||
|
||||
test('should not re-execute previous commands in the chain before `try`', () => {
|
||||
const command1: Command1 = vi.fn((_ctx, next) =>
|
||||
next({ commandData1: '123' })
|
||||
);
|
||||
const command2: Command = vi.fn((_ctx, _next) => {});
|
||||
const command3: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success, ctx] = commandManager
|
||||
.chain()
|
||||
.command1()
|
||||
.try(cmd => [cmd.command2(), cmd.command3()])
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalledTimes(1);
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
expect(ctx.commandData1).toBe('123');
|
||||
});
|
||||
|
||||
test('should continue with the rest of the chain if one command in `try` succeeds', () => {
|
||||
const command1: Command1 = vi.fn((_ctx, _next) => {});
|
||||
const command2: Command2 = vi.fn((_ctx, next) =>
|
||||
next({ commandData2: '123' })
|
||||
);
|
||||
const command3: Command3 = vi.fn((_ctx, next) =>
|
||||
next({ commandData3: '456' })
|
||||
);
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success, ctx] = commandManager
|
||||
.chain()
|
||||
.try(cmd => [cmd.command1(), cmd.command2()])
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
expect(ctx.commandData1).toBeUndefined();
|
||||
expect(ctx.commandData2).toBe('123');
|
||||
expect(ctx.commandData3).toBe('456');
|
||||
});
|
||||
|
||||
test('should not execute any further commands in `try` after one succeeds', () => {
|
||||
const command1: Command1 = vi.fn((_ctx, next) =>
|
||||
next({ commandData1: '123' })
|
||||
);
|
||||
const command2: Command2 = vi.fn((_ctx, next) =>
|
||||
next({ commandData2: '456' })
|
||||
);
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
|
||||
const [success, ctx] = commandManager
|
||||
.chain()
|
||||
.try(cmd => [cmd.command1(), cmd.command2()])
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).not.toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
expect(ctx.commandData1).toBe('123');
|
||||
expect(ctx.commandData2).toBeUndefined();
|
||||
});
|
||||
|
||||
test('should pass context correctly in `try` when a command succeeds', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) =>
|
||||
next({ commandData1: 'fromCommand1', commandData2: 'fromCommand1' })
|
||||
);
|
||||
const command2: Command<'commandData1' | 'commandData2'> = vi.fn(
|
||||
(ctx, next) => {
|
||||
expect(ctx.commandData1).toBe('fromCommand1');
|
||||
expect(ctx.commandData2).toBe('fromCommand1');
|
||||
// override commandData2
|
||||
next({ commandData2: 'fromCommand2' });
|
||||
}
|
||||
);
|
||||
const command3: Command<'commandData1' | 'commandData2'> = vi.fn(
|
||||
(ctx, next) => {
|
||||
expect(ctx.commandData1).toBe('fromCommand1');
|
||||
expect(ctx.commandData2).toBe('fromCommand2');
|
||||
next();
|
||||
}
|
||||
);
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.command1()
|
||||
.try(cmd => [cmd.command2()])
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should continue with the rest of the chain if at least one command in `tryAll` succeeds', () => {
|
||||
const command1: Command = vi.fn((_ctx, _next) => {});
|
||||
const command2: Command = vi.fn((_ctx, next) => next());
|
||||
const command3: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.tryAll(cmd => [cmd.command1(), cmd.command2()])
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should execute all commands in `tryAll` even if one has already succeeded', () => {
|
||||
const command1: Command1 = vi.fn((_ctx, next) =>
|
||||
next({ commandData1: '123' })
|
||||
);
|
||||
const command2: Command2 = vi.fn((_ctx, next) =>
|
||||
next({ commandData2: '456' })
|
||||
);
|
||||
const command3: Command3 = vi.fn((_ctx, next) =>
|
||||
next({ commandData3: '789' })
|
||||
);
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success, ctx] = commandManager
|
||||
.chain()
|
||||
.tryAll(cmd => [cmd.command1(), cmd.command2(), cmd.command3()])
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(ctx.commandData1).toBe('123');
|
||||
expect(ctx.commandData2).toBe('456');
|
||||
expect(ctx.commandData3).toBe('789');
|
||||
expect(success).toBeTruthy();
|
||||
});
|
||||
|
||||
test('should not continue with the rest of the chain if all commands in `tryAll` fail', () => {
|
||||
const command1: Command1 = vi.fn((_ctx, _next) => {});
|
||||
const command2: Command2 = vi.fn((_ctx, _next) => {});
|
||||
const command3: Command3 = vi.fn((_ctx, next) =>
|
||||
next({ commandData3: '123' })
|
||||
);
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
|
||||
const [success, ctx] = commandManager
|
||||
.chain()
|
||||
.tryAll(cmd => [cmd.command1(), cmd.command2()])
|
||||
.command3()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).not.toHaveBeenCalled();
|
||||
expect(ctx.commandData3).toBeUndefined();
|
||||
expect(success).toBeFalsy();
|
||||
});
|
||||
|
||||
test('should pass context correctly in `tryAll` when at least one command succeeds', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) =>
|
||||
next({ commandData1: 'fromCommand1' })
|
||||
);
|
||||
const command2: Command<'commandData1'> = vi.fn((ctx, next) => {
|
||||
expect(ctx.commandData1).toBe('fromCommand1');
|
||||
// override commandData1
|
||||
next({ commandData1: 'fromCommand2', commandData2: 'fromCommand2' });
|
||||
});
|
||||
const command3: Command<'commandData1' | 'commandData2'> = vi.fn(
|
||||
(ctx, next) => {
|
||||
expect(ctx.commandData1).toBe('fromCommand2');
|
||||
expect(ctx.commandData2).toBe('fromCommand2');
|
||||
next({
|
||||
// override commandData2
|
||||
commandData2: 'fromCommand3',
|
||||
commandData3: 'fromCommand3',
|
||||
});
|
||||
}
|
||||
);
|
||||
const command4: Command<'commandData1' | 'commandData2' | 'commandData3'> =
|
||||
vi.fn((ctx, next) => {
|
||||
expect(ctx.commandData1).toBe('fromCommand2');
|
||||
expect(ctx.commandData2).toBe('fromCommand3');
|
||||
expect(ctx.commandData3).toBe('fromCommand3');
|
||||
next();
|
||||
});
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
commandManager.add('command4', command4);
|
||||
|
||||
const [success, ctx] = commandManager
|
||||
.chain()
|
||||
.command1()
|
||||
.tryAll(cmd => [cmd.command2(), cmd.command3()])
|
||||
.command4()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalled();
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(command4).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
expect(ctx.commandData1).toBe('fromCommand2');
|
||||
expect(ctx.commandData2).toBe('fromCommand3');
|
||||
expect(ctx.commandData3).toBe('fromCommand3');
|
||||
});
|
||||
|
||||
test('should not re-execute commands before `tryAll` after executing `tryAll`', () => {
|
||||
const command1: Command = vi.fn((_ctx, next) => next());
|
||||
const command2: Command = vi.fn((_ctx, next) => next());
|
||||
const command3: Command = vi.fn((_ctx, _next) => {});
|
||||
const command4: Command = vi.fn((_ctx, next) => next());
|
||||
|
||||
commandManager.add('command1', command1);
|
||||
commandManager.add('command2', command2);
|
||||
commandManager.add('command3', command3);
|
||||
commandManager.add('command4', command4);
|
||||
|
||||
const [success] = commandManager
|
||||
.chain()
|
||||
.command1()
|
||||
.tryAll(cmd => [cmd.command2(), cmd.command3()])
|
||||
.command4()
|
||||
.run();
|
||||
|
||||
expect(command1).toHaveBeenCalledTimes(1);
|
||||
expect(command2).toHaveBeenCalled();
|
||||
expect(command3).toHaveBeenCalled();
|
||||
expect(command4).toHaveBeenCalled();
|
||||
expect(success).toBeTruthy();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,56 @@
|
||||
import { DocCollection, IdGeneratorType, Schema } from '@blocksuite/store';
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { effects } from '../effects.js';
|
||||
import { TestEditorContainer } from './test-editor.js';
|
||||
import {
|
||||
type HeadingBlockModel,
|
||||
HeadingBlockSchema,
|
||||
NoteBlockSchema,
|
||||
RootBlockSchema,
|
||||
} from './test-schema.js';
|
||||
import { testSpecs } from './test-spec.js';
|
||||
|
||||
effects();
|
||||
|
||||
function createTestOptions() {
|
||||
const idGenerator = IdGeneratorType.AutoIncrement;
|
||||
const schema = new Schema();
|
||||
schema.register([RootBlockSchema, NoteBlockSchema, HeadingBlockSchema]);
|
||||
return { id: 'test-collection', idGenerator, schema };
|
||||
}
|
||||
|
||||
function wait(time: number) {
|
||||
return new Promise(resolve => setTimeout(resolve, time));
|
||||
}
|
||||
|
||||
describe('editor host', () => {
|
||||
test('editor host should rerender model when view changes', async () => {
|
||||
const collection = new DocCollection(createTestOptions());
|
||||
|
||||
collection.meta.initialize();
|
||||
const doc = collection.createDoc({ id: 'home' });
|
||||
doc.load();
|
||||
const rootId = doc.addBlock('test:page');
|
||||
const noteId = doc.addBlock('test:note', {}, rootId);
|
||||
const headingId = doc.addBlock('test:heading', { type: 'h1' }, noteId);
|
||||
const headingBlock = doc.getBlock(headingId)!;
|
||||
|
||||
const editorContainer = new TestEditorContainer();
|
||||
editorContainer.doc = doc;
|
||||
editorContainer.specs = testSpecs;
|
||||
|
||||
document.body.append(editorContainer);
|
||||
|
||||
await wait(50);
|
||||
let headingElm = editorContainer.std.view.getBlock(headingId);
|
||||
|
||||
expect(headingElm!.tagName).toBe('TEST-H1-BLOCK');
|
||||
|
||||
(headingBlock.model as HeadingBlockModel).type = 'h2';
|
||||
await wait(50);
|
||||
headingElm = editorContainer.std.view.getBlock(headingId);
|
||||
|
||||
expect(headingElm!.tagName).toBe('TEST-H2-BLOCK');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,65 @@
|
||||
import rehypeParse from 'rehype-parse';
|
||||
import { unified } from 'unified';
|
||||
import { describe, expect, test } from 'vitest';
|
||||
|
||||
import { onlyContainImgElement } from '../clipboard/index.js';
|
||||
|
||||
describe('only contains img elements', () => {
|
||||
test('normal with head', () => {
|
||||
const htmlAst = unified().use(rehypeParse).parse(`<html>
|
||||
<head></head>
|
||||
<body>
|
||||
<!--StartFragment--><img src="https://files.slack.com/deadbeef.png" alt="image.png"/><!--EndFragment-->
|
||||
</body>
|
||||
</html>`);
|
||||
const isImgOnly =
|
||||
htmlAst.children.map(onlyContainImgElement).reduce((a, b) => {
|
||||
if (a === 'no' || b === 'no') {
|
||||
return 'no';
|
||||
}
|
||||
if (a === 'maybe' && b === 'maybe') {
|
||||
return 'maybe';
|
||||
}
|
||||
return 'yes';
|
||||
}, 'maybe') === 'yes';
|
||||
expect(isImgOnly).toBe(true);
|
||||
});
|
||||
|
||||
test('normal without head', () => {
|
||||
const htmlAst = unified().use(rehypeParse).parse(`<html>
|
||||
<body>
|
||||
<!--StartFragment--><img src="https://files.slack.com/deadbeef.png" alt="image.png"/><!--EndFragment-->
|
||||
</body>
|
||||
</html>`);
|
||||
const isImgOnly =
|
||||
htmlAst.children.map(onlyContainImgElement).reduce((a, b) => {
|
||||
if (a === 'no' || b === 'no') {
|
||||
return 'no';
|
||||
}
|
||||
if (a === 'maybe' && b === 'maybe') {
|
||||
return 'maybe';
|
||||
}
|
||||
return 'yes';
|
||||
}, 'maybe') === 'yes';
|
||||
expect(isImgOnly).toBe(true);
|
||||
});
|
||||
|
||||
test('contain spans', () => {
|
||||
const htmlAst = unified().use(rehypeParse).parse(`<html>
|
||||
<body>
|
||||
<!--StartFragment--><img src="https://files.slack.com/deadbeef.png" alt="image.png"/><span></span><!--EndFragment-->
|
||||
</body>
|
||||
</html>`);
|
||||
const isImgOnly =
|
||||
htmlAst.children.map(onlyContainImgElement).reduce((a, b) => {
|
||||
if (a === 'no' || b === 'no') {
|
||||
return 'no';
|
||||
}
|
||||
if (a === 'maybe' && b === 'maybe') {
|
||||
return 'maybe';
|
||||
}
|
||||
return 'yes';
|
||||
}, 'maybe') === 'yes';
|
||||
expect(isImgOnly).toBe(false);
|
||||
});
|
||||
});
|
||||
41
blocksuite/framework/block-std/src/__tests__/test-block.ts
Normal file
41
blocksuite/framework/block-std/src/__tests__/test-block.ts
Normal file
@@ -0,0 +1,41 @@
|
||||
import { html } from 'lit';
|
||||
import { customElement } from 'lit/decorators.js';
|
||||
|
||||
import { BlockComponent } from '../view/index.js';
|
||||
import type {
|
||||
HeadingBlockModel,
|
||||
NoteBlockModel,
|
||||
RootBlockModel,
|
||||
} from './test-schema.js';
|
||||
|
||||
@customElement('test-root-block')
|
||||
export class RootBlockComponent extends BlockComponent<RootBlockModel> {
|
||||
override renderBlock() {
|
||||
return html`
|
||||
<div class="test-root-block">${this.renderChildren(this.model)}</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
@customElement('test-note-block')
|
||||
export class NoteBlockComponent extends BlockComponent<NoteBlockModel> {
|
||||
override renderBlock() {
|
||||
return html`
|
||||
<div class="test-note-block">${this.renderChildren(this.model)}</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
@customElement('test-h1-block')
|
||||
export class HeadingH1BlockComponent extends BlockComponent<HeadingBlockModel> {
|
||||
override renderBlock() {
|
||||
return html` <div class="test-heading-block h1">${this.model.text}</div> `;
|
||||
}
|
||||
}
|
||||
|
||||
@customElement('test-h2-block')
|
||||
export class HeadingH2BlockComponent extends BlockComponent<HeadingBlockModel> {
|
||||
override renderBlock() {
|
||||
return html` <div class="test-heading-block h2">${this.model.text}</div> `;
|
||||
}
|
||||
}
|
||||
39
blocksuite/framework/block-std/src/__tests__/test-editor.ts
Normal file
39
blocksuite/framework/block-std/src/__tests__/test-editor.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
import { SignalWatcher, WithDisposable } from '@blocksuite/global/utils';
|
||||
import type { Doc } from '@blocksuite/store';
|
||||
import { html } from 'lit';
|
||||
import { customElement, property } from 'lit/decorators.js';
|
||||
|
||||
import type { ExtensionType } from '../extension/index.js';
|
||||
import { BlockStdScope } from '../scope/index.js';
|
||||
import { ShadowlessElement } from '../view/index.js';
|
||||
|
||||
@customElement('test-editor-container')
|
||||
export class TestEditorContainer extends SignalWatcher(
|
||||
WithDisposable(ShadowlessElement)
|
||||
) {
|
||||
private _std!: BlockStdScope;
|
||||
|
||||
get std() {
|
||||
return this._std;
|
||||
}
|
||||
|
||||
override connectedCallback() {
|
||||
super.connectedCallback();
|
||||
this._std = new BlockStdScope({
|
||||
doc: this.doc,
|
||||
extensions: this.specs,
|
||||
});
|
||||
}
|
||||
|
||||
protected override render() {
|
||||
return html` <div class="test-editor-container">
|
||||
${this._std.render()}
|
||||
</div>`;
|
||||
}
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor doc!: Doc;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor specs: ExtensionType[] = [];
|
||||
}
|
||||
56
blocksuite/framework/block-std/src/__tests__/test-schema.ts
Normal file
56
blocksuite/framework/block-std/src/__tests__/test-schema.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { defineBlockSchema, type SchemaToModel } from '@blocksuite/store';
|
||||
|
||||
export const RootBlockSchema = defineBlockSchema({
|
||||
flavour: 'test:page',
|
||||
props: internal => ({
|
||||
title: internal.Text(),
|
||||
count: 0,
|
||||
style: {} as Record<string, unknown>,
|
||||
items: [] as unknown[],
|
||||
}),
|
||||
metadata: {
|
||||
version: 2,
|
||||
role: 'root',
|
||||
children: ['test:note'],
|
||||
},
|
||||
});
|
||||
|
||||
export type RootBlockModel = SchemaToModel<typeof RootBlockSchema>;
|
||||
|
||||
export const NoteBlockSchema = defineBlockSchema({
|
||||
flavour: 'test:note',
|
||||
props: () => ({}),
|
||||
metadata: {
|
||||
version: 1,
|
||||
role: 'hub',
|
||||
parent: ['test:page'],
|
||||
children: ['test:heading'],
|
||||
},
|
||||
});
|
||||
|
||||
export type NoteBlockModel = SchemaToModel<typeof NoteBlockSchema>;
|
||||
|
||||
export const HeadingBlockSchema = defineBlockSchema({
|
||||
flavour: 'test:heading',
|
||||
props: internal => ({
|
||||
type: 'h1',
|
||||
text: internal.Text(),
|
||||
}),
|
||||
metadata: {
|
||||
version: 1,
|
||||
role: 'content',
|
||||
parent: ['test:note'],
|
||||
},
|
||||
});
|
||||
|
||||
export type HeadingBlockModel = SchemaToModel<typeof HeadingBlockSchema>;
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface BlockModels {
|
||||
'test:page': RootBlockModel;
|
||||
'test:note': NoteBlockModel;
|
||||
'test:heading': HeadingBlockModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
22
blocksuite/framework/block-std/src/__tests__/test-spec.ts
Normal file
22
blocksuite/framework/block-std/src/__tests__/test-spec.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import './test-block.js';
|
||||
|
||||
import { literal } from 'lit/static-html.js';
|
||||
|
||||
import { BlockViewExtension, type ExtensionType } from '../extension/index.js';
|
||||
import type { HeadingBlockModel } from './test-schema.js';
|
||||
|
||||
export const testSpecs: ExtensionType[] = [
|
||||
BlockViewExtension('test:page', literal`test-root-block`),
|
||||
|
||||
BlockViewExtension('test:note', literal`test-note-block`),
|
||||
|
||||
BlockViewExtension('test:heading', model => {
|
||||
const h = (model as HeadingBlockModel).type$.value;
|
||||
|
||||
if (h === 'h1') {
|
||||
return literal`test-h1-block`;
|
||||
}
|
||||
|
||||
return literal`test-h2-block`;
|
||||
}),
|
||||
];
|
||||
363
blocksuite/framework/block-std/src/clipboard/index.ts
Normal file
363
blocksuite/framework/block-std/src/clipboard/index.ts
Normal file
@@ -0,0 +1,363 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import type {
|
||||
BaseAdapter,
|
||||
BlockSnapshot,
|
||||
Doc,
|
||||
JobMiddleware,
|
||||
Slice,
|
||||
} from '@blocksuite/store';
|
||||
import { Job } from '@blocksuite/store';
|
||||
import DOMPurify from 'dompurify';
|
||||
import type { RootContentMap } from 'hast';
|
||||
import * as lz from 'lz-string';
|
||||
import rehypeParse from 'rehype-parse';
|
||||
import { unified } from 'unified';
|
||||
|
||||
import { LifeCycleWatcher } from '../extension/index.js';
|
||||
|
||||
type AdapterConstructor<T extends BaseAdapter> = new (job: Job) => T;
|
||||
|
||||
type AdapterMap = Map<
|
||||
string,
|
||||
{
|
||||
adapter: AdapterConstructor<BaseAdapter>;
|
||||
priority: number;
|
||||
}
|
||||
>;
|
||||
|
||||
type HastUnionType<
|
||||
K extends keyof RootContentMap,
|
||||
V extends RootContentMap[K],
|
||||
> = V;
|
||||
|
||||
export function onlyContainImgElement(
|
||||
ast: HastUnionType<keyof RootContentMap, RootContentMap[keyof RootContentMap]>
|
||||
): 'yes' | 'no' | 'maybe' {
|
||||
if (ast.type === 'element') {
|
||||
switch (ast.tagName) {
|
||||
case 'html':
|
||||
case 'body':
|
||||
return ast.children.map(onlyContainImgElement).reduce((a, b) => {
|
||||
if (a === 'no' || b === 'no') {
|
||||
return 'no';
|
||||
}
|
||||
if (a === 'maybe' && b === 'maybe') {
|
||||
return 'maybe';
|
||||
}
|
||||
return 'yes';
|
||||
}, 'maybe');
|
||||
case 'img':
|
||||
return 'yes';
|
||||
case 'head':
|
||||
return 'maybe';
|
||||
default:
|
||||
return 'no';
|
||||
}
|
||||
}
|
||||
return 'maybe';
|
||||
}
|
||||
|
||||
export class Clipboard extends LifeCycleWatcher {
|
||||
static override readonly key = 'clipboard';
|
||||
|
||||
private _adapterMap: AdapterMap = new Map();
|
||||
|
||||
// Need to be cloned to a map for later use
|
||||
private _getDataByType = (clipboardData: DataTransfer) => {
|
||||
const data = new Map<string, string | File[]>();
|
||||
for (const type of clipboardData.types) {
|
||||
if (type === 'Files') {
|
||||
data.set(type, Array.from(clipboardData.files));
|
||||
} else {
|
||||
data.set(type, clipboardData.getData(type));
|
||||
}
|
||||
}
|
||||
if (data.get('Files') && data.get('text/html')) {
|
||||
const htmlAst = unified()
|
||||
.use(rehypeParse)
|
||||
.parse(data.get('text/html') as string);
|
||||
|
||||
const isImgOnly =
|
||||
htmlAst.children.map(onlyContainImgElement).reduce((a, b) => {
|
||||
if (a === 'no' || b === 'no') {
|
||||
return 'no';
|
||||
}
|
||||
if (a === 'maybe' && b === 'maybe') {
|
||||
return 'maybe';
|
||||
}
|
||||
return 'yes';
|
||||
}, 'maybe') === 'yes';
|
||||
|
||||
if (isImgOnly) {
|
||||
data.delete('text/html');
|
||||
}
|
||||
}
|
||||
return (type: string) => {
|
||||
const item = data.get(type);
|
||||
if (item) {
|
||||
return item;
|
||||
}
|
||||
const files = (data.get('Files') ?? []) as File[];
|
||||
if (files.length > 0) {
|
||||
return files;
|
||||
}
|
||||
return '';
|
||||
};
|
||||
};
|
||||
|
||||
private _getSnapshotByPriority = async (
|
||||
getItem: (type: string) => string | File[],
|
||||
doc: Doc,
|
||||
parent?: string,
|
||||
index?: number
|
||||
) => {
|
||||
const byPriority = Array.from(this._adapterMap.entries()).sort(
|
||||
(a, b) => b[1].priority - a[1].priority
|
||||
);
|
||||
for (const [type, { adapter }] of byPriority) {
|
||||
const item = getItem(type);
|
||||
if (Array.isArray(item)) {
|
||||
if (item.length === 0) {
|
||||
continue;
|
||||
}
|
||||
if (
|
||||
// if all files are not the same target type, fallback to */*
|
||||
!item
|
||||
.map(f => f.type === type || type === '*/*')
|
||||
.reduce((a, b) => a && b, true)
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (item) {
|
||||
const job = this._getJob();
|
||||
const adapterInstance = new adapter(job);
|
||||
const payload = {
|
||||
file: item,
|
||||
assets: job.assetsManager,
|
||||
blockVersions: doc.collection.meta.blockVersions,
|
||||
workspaceId: doc.collection.id,
|
||||
pageId: doc.id,
|
||||
};
|
||||
const result = await adapterInstance.toSlice(
|
||||
payload,
|
||||
doc,
|
||||
parent,
|
||||
index
|
||||
);
|
||||
if (result) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
private _jobMiddlewares: JobMiddleware[] = [];
|
||||
|
||||
copy = async (slice: Slice) => {
|
||||
return this.copySlice(slice);
|
||||
};
|
||||
|
||||
// Gated by https://developer.mozilla.org/en-US/docs/Glossary/Transient_activation
|
||||
copySlice = async (slice: Slice) => {
|
||||
const adapterKeys = Array.from(this._adapterMap.keys());
|
||||
|
||||
await this.writeToClipboard(async _items => {
|
||||
const items = { ..._items };
|
||||
|
||||
await Promise.all(
|
||||
adapterKeys.map(async type => {
|
||||
const item = await this._getClipboardItem(slice, type);
|
||||
if (typeof item === 'string') {
|
||||
items[type] = item;
|
||||
}
|
||||
})
|
||||
);
|
||||
return items;
|
||||
});
|
||||
};
|
||||
|
||||
duplicateSlice = async (
|
||||
slice: Slice,
|
||||
doc: Doc,
|
||||
parent?: string,
|
||||
index?: number,
|
||||
type = 'BLOCKSUITE/SNAPSHOT'
|
||||
) => {
|
||||
const items = {
|
||||
[type]: await this._getClipboardItem(slice, type),
|
||||
};
|
||||
|
||||
await this._getSnapshotByPriority(
|
||||
type => (items[type] as string | File[]) ?? '',
|
||||
doc,
|
||||
parent,
|
||||
index
|
||||
);
|
||||
};
|
||||
|
||||
paste = async (
|
||||
event: ClipboardEvent,
|
||||
doc: Doc,
|
||||
parent?: string,
|
||||
index?: number
|
||||
) => {
|
||||
const data = event.clipboardData;
|
||||
if (!data) return;
|
||||
|
||||
try {
|
||||
const json = this.readFromClipboard(data);
|
||||
const slice = await this._getSnapshotByPriority(
|
||||
type => json[type],
|
||||
doc,
|
||||
parent,
|
||||
index
|
||||
);
|
||||
if (!slice) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.TransformerError,
|
||||
'No snapshot found'
|
||||
);
|
||||
}
|
||||
return slice;
|
||||
} catch {
|
||||
const getDataByType = this._getDataByType(data);
|
||||
const slice = await this._getSnapshotByPriority(
|
||||
type => getDataByType(type),
|
||||
doc,
|
||||
parent,
|
||||
index
|
||||
);
|
||||
|
||||
return slice;
|
||||
}
|
||||
};
|
||||
|
||||
pasteBlockSnapshot = async (
|
||||
snapshot: BlockSnapshot,
|
||||
doc: Doc,
|
||||
parent?: string,
|
||||
index?: number
|
||||
) => {
|
||||
return this._getJob().snapshotToBlock(snapshot, doc, parent, index);
|
||||
};
|
||||
|
||||
registerAdapter = <T extends BaseAdapter>(
|
||||
mimeType: string,
|
||||
adapter: AdapterConstructor<T>,
|
||||
priority = 0
|
||||
) => {
|
||||
this._adapterMap.set(mimeType, { adapter, priority });
|
||||
};
|
||||
|
||||
unregisterAdapter = (mimeType: string) => {
|
||||
this._adapterMap.delete(mimeType);
|
||||
};
|
||||
|
||||
unuse = (middleware: JobMiddleware) => {
|
||||
this._jobMiddlewares = this._jobMiddlewares.filter(m => m !== middleware);
|
||||
};
|
||||
|
||||
use = (middleware: JobMiddleware) => {
|
||||
this._jobMiddlewares.push(middleware);
|
||||
};
|
||||
|
||||
get configs() {
|
||||
return this._getJob().adapterConfigs;
|
||||
}
|
||||
|
||||
private async _getClipboardItem(slice: Slice, type: string) {
|
||||
const job = this._getJob();
|
||||
const adapterItem = this._adapterMap.get(type);
|
||||
if (!adapterItem) {
|
||||
return;
|
||||
}
|
||||
const { adapter } = adapterItem;
|
||||
const adapterInstance = new adapter(job);
|
||||
const result = await adapterInstance.fromSlice(slice);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
return result.file;
|
||||
}
|
||||
|
||||
private _getJob() {
|
||||
return new Job({
|
||||
middlewares: this._jobMiddlewares,
|
||||
collection: this.std.collection,
|
||||
});
|
||||
}
|
||||
|
||||
readFromClipboard(clipboardData: DataTransfer) {
|
||||
const items = clipboardData.getData('text/html');
|
||||
const sanitizedItems = DOMPurify.sanitize(items);
|
||||
const domParser = new DOMParser();
|
||||
const doc = domParser.parseFromString(sanitizedItems, 'text/html');
|
||||
const dom = doc.querySelector<HTMLDivElement>('[data-blocksuite-snapshot]');
|
||||
if (!dom) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.TransformerError,
|
||||
'No snapshot found'
|
||||
);
|
||||
}
|
||||
const json = JSON.parse(
|
||||
lz.decompressFromEncodedURIComponent(
|
||||
dom.dataset.blocksuiteSnapshot as string
|
||||
)
|
||||
);
|
||||
return json;
|
||||
}
|
||||
|
||||
sliceToSnapshot(slice: Slice) {
|
||||
const job = this._getJob();
|
||||
return job.sliceToSnapshot(slice);
|
||||
}
|
||||
|
||||
async writeToClipboard(
|
||||
updateItems: (
|
||||
items: Record<string, unknown>
|
||||
) => Promise<Record<string, unknown>> | Record<string, unknown>
|
||||
) {
|
||||
const _items = {
|
||||
'text/plain': '',
|
||||
'text/html': '',
|
||||
'image/png': '',
|
||||
};
|
||||
|
||||
const items = await updateItems(_items);
|
||||
|
||||
const text = items['text/plain'] as string;
|
||||
const innerHTML = items['text/html'] as string;
|
||||
const png = items['image/png'] as string | Blob;
|
||||
|
||||
delete items['text/plain'];
|
||||
delete items['text/html'];
|
||||
delete items['image/png'];
|
||||
|
||||
const snapshot = lz.compressToEncodedURIComponent(JSON.stringify(items));
|
||||
const html = `<div data-blocksuite-snapshot='${snapshot}'>${innerHTML}</div>`;
|
||||
const htmlBlob = new Blob([html], {
|
||||
type: 'text/html',
|
||||
});
|
||||
const clipboardItems: Record<string, Blob> = {
|
||||
'text/html': htmlBlob,
|
||||
};
|
||||
if (text.length > 0) {
|
||||
const textBlob = new Blob([text], {
|
||||
type: 'text/plain',
|
||||
});
|
||||
clipboardItems['text/plain'] = textBlob;
|
||||
}
|
||||
|
||||
if (png instanceof Blob) {
|
||||
clipboardItems['image/png'] = png;
|
||||
} else if (png.length > 0) {
|
||||
const pngBlob = new Blob([png], {
|
||||
type: 'image/png',
|
||||
});
|
||||
clipboardItems['image/png'] = pngBlob;
|
||||
}
|
||||
await navigator.clipboard.write([new ClipboardItem(clipboardItems)]);
|
||||
}
|
||||
}
|
||||
1
blocksuite/framework/block-std/src/command/consts.ts
Normal file
1
blocksuite/framework/block-std/src/command/consts.ts
Normal file
@@ -0,0 +1 @@
|
||||
export const cmdSymbol = Symbol('cmds');
|
||||
3
blocksuite/framework/block-std/src/command/index.ts
Normal file
3
blocksuite/framework/block-std/src/command/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export * from './consts.js';
|
||||
export * from './manager.js';
|
||||
export * from './types.js';
|
||||
382
blocksuite/framework/block-std/src/command/manager.ts
Normal file
382
blocksuite/framework/block-std/src/command/manager.ts
Normal file
@@ -0,0 +1,382 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
import { LifeCycleWatcher } from '../extension/index.js';
|
||||
import { CommandIdentifier } from '../identifier.js';
|
||||
import { cmdSymbol } from './consts.js';
|
||||
import type {
|
||||
Chain,
|
||||
Command,
|
||||
ExecCommandResult,
|
||||
IfAllKeysOptional,
|
||||
InDataOfCommand,
|
||||
InitCommandCtx,
|
||||
} from './types.js';
|
||||
|
||||
/**
|
||||
* Command manager to manage all commands
|
||||
*
|
||||
* Commands are functions that take a context and a next function as arguments
|
||||
*
|
||||
* ```ts
|
||||
* const myCommand: Command<'count', 'count'> = (ctx, next) => {
|
||||
* const count = ctx.count || 0;
|
||||
*
|
||||
* const success = someOperation();
|
||||
* if (success) {
|
||||
* return next({ count: count + 1 });
|
||||
* }
|
||||
* // if the command is not successful, you can return without calling next
|
||||
* return;
|
||||
* ```
|
||||
*
|
||||
* You should always add the command to the global interface `BlockSuite.Commands`
|
||||
* ```ts
|
||||
* declare global {
|
||||
* namespace BlockSuite {
|
||||
* interface Commands {
|
||||
* 'myCommand': typeof myCommand
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* Command input and output data can be defined in the `Command` type
|
||||
*
|
||||
* ```ts
|
||||
* // input: ctx.firstName, ctx.lastName
|
||||
* // output: ctx.fullName
|
||||
* const myCommand: Command<'firstName' | 'lastName', 'fullName'> = (ctx, next) => {
|
||||
* const { firstName, lastName } = ctx;
|
||||
* const fullName = `${firstName} ${lastName}`;
|
||||
* return next({ fullName });
|
||||
* }
|
||||
*
|
||||
* declare global {
|
||||
* namespace BlockSuite {
|
||||
* interface CommandContext {
|
||||
* // All command input and output data should be defined here
|
||||
* // The keys should be optional
|
||||
* firstName?: string;
|
||||
* lastName?: string;
|
||||
* fullName?: string;
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
*
|
||||
* ```
|
||||
*
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* Commands can be run in two ways:
|
||||
*
|
||||
* 1. Using `exec` method
|
||||
* `exec` is used to run a single command
|
||||
* ```ts
|
||||
* const { success, ...data } = commandManager.exec('myCommand', payload);
|
||||
* ```
|
||||
*
|
||||
* 2. Using `chain` method
|
||||
* `chain` is used to run a series of commands
|
||||
* ```ts
|
||||
* const chain = commandManager.chain();
|
||||
* const [result, data] = chain
|
||||
* .myCommand1()
|
||||
* .myCommand2(payload)
|
||||
* .run();
|
||||
* ```
|
||||
*
|
||||
* ---
|
||||
*
|
||||
* Command chains will stop running if a command is not successful
|
||||
*
|
||||
* ```ts
|
||||
* const chain = commandManager.chain();
|
||||
* const [result, data] = chain
|
||||
* .myCommand1() <-- if this fail
|
||||
* .myCommand2(payload) <- this won't run
|
||||
* .run();
|
||||
*
|
||||
* result <- result will be `false`
|
||||
* ```
|
||||
*
|
||||
* You can use `try` to run a series of commands and if one of them is successful, it will continue to the next command
|
||||
* ```ts
|
||||
* const chain = commandManager.chain();
|
||||
* const [result, data] = chain
|
||||
* .try(chain => [
|
||||
* chain.myCommand1(), <- if this fail
|
||||
* chain.myCommand2(), <- this will run, if this success
|
||||
* chain.myCommand3(), <- this won't run
|
||||
* ])
|
||||
* .run();
|
||||
* ```
|
||||
*
|
||||
* The `tryAll` method is similar to `try`, but it will run all commands even if one of them is successful
|
||||
* ```ts
|
||||
* const chain = commandManager.chain();
|
||||
* const [result, data] = chain
|
||||
* .try(chain => [
|
||||
* chain.myCommand1(), <- if this success
|
||||
* chain.myCommand2(), <- this will also run
|
||||
* chain.myCommand3(), <- so will this
|
||||
* ])
|
||||
* .run();
|
||||
* ```
|
||||
*
|
||||
*/
|
||||
export class CommandManager extends LifeCycleWatcher {
|
||||
static override readonly key = 'commandManager';
|
||||
|
||||
private _commands = new Map<string, Command>();
|
||||
|
||||
private _createChain = (
|
||||
methods: Record<BlockSuite.CommandName, unknown>,
|
||||
_cmds: Command[]
|
||||
): Chain => {
|
||||
const getCommandCtx = this._getCommandCtx;
|
||||
const createChain = this._createChain;
|
||||
const chain = this.chain;
|
||||
|
||||
return {
|
||||
[cmdSymbol]: _cmds,
|
||||
run: function (this: Chain) {
|
||||
let ctx = getCommandCtx();
|
||||
let success = false;
|
||||
try {
|
||||
const cmds = this[cmdSymbol];
|
||||
ctx = runCmds(ctx as BlockSuite.CommandContext, [
|
||||
...cmds,
|
||||
(_, next) => {
|
||||
success = true;
|
||||
next();
|
||||
},
|
||||
]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
}
|
||||
|
||||
return [success, ctx];
|
||||
},
|
||||
with: function (this: Chain, value) {
|
||||
const cmds = this[cmdSymbol];
|
||||
return createChain(methods, [
|
||||
...cmds,
|
||||
(_, next) => next(value),
|
||||
]) as never;
|
||||
},
|
||||
inline: function (this: Chain, command) {
|
||||
const cmds = this[cmdSymbol];
|
||||
return createChain(methods, [...cmds, command]) as never;
|
||||
},
|
||||
try: function (this: Chain, fn) {
|
||||
const cmds = this[cmdSymbol];
|
||||
return createChain(methods, [
|
||||
...cmds,
|
||||
(beforeCtx, next) => {
|
||||
let ctx = beforeCtx;
|
||||
const chains = fn(chain());
|
||||
|
||||
chains.some(chain => {
|
||||
// inject ctx in the beginning
|
||||
chain[cmdSymbol] = [
|
||||
(_, next) => {
|
||||
next(ctx);
|
||||
},
|
||||
...chain[cmdSymbol],
|
||||
];
|
||||
|
||||
const [success] = chain
|
||||
.inline((branchCtx, next) => {
|
||||
ctx = { ...ctx, ...branchCtx };
|
||||
next();
|
||||
})
|
||||
.run();
|
||||
if (success) {
|
||||
next(ctx);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
});
|
||||
},
|
||||
]) as never;
|
||||
},
|
||||
tryAll: function (this: Chain, fn) {
|
||||
const cmds = this[cmdSymbol];
|
||||
return createChain(methods, [
|
||||
...cmds,
|
||||
(beforeCtx, next) => {
|
||||
let ctx = beforeCtx;
|
||||
const chains = fn(chain());
|
||||
|
||||
let allFail = true;
|
||||
chains.forEach(chain => {
|
||||
// inject ctx in the beginning
|
||||
chain[cmdSymbol] = [
|
||||
(_, next) => {
|
||||
next(ctx);
|
||||
},
|
||||
...chain[cmdSymbol],
|
||||
];
|
||||
|
||||
const [success] = chain
|
||||
.inline((branchCtx, next) => {
|
||||
ctx = { ...ctx, ...branchCtx };
|
||||
next();
|
||||
})
|
||||
.run();
|
||||
if (success) {
|
||||
allFail = false;
|
||||
}
|
||||
});
|
||||
if (!allFail) {
|
||||
next(ctx);
|
||||
}
|
||||
},
|
||||
]) as never;
|
||||
},
|
||||
...methods,
|
||||
} as Chain;
|
||||
};
|
||||
|
||||
private _getCommandCtx = (): InitCommandCtx => {
|
||||
return {
|
||||
std: this.std,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Create a chain to run a series of commands
|
||||
* ```ts
|
||||
* const chain = commandManager.chain();
|
||||
* const [result, data] = chain
|
||||
* .myCommand1()
|
||||
* .myCommand2(payload)
|
||||
* .run();
|
||||
* ```
|
||||
* @returns [success, data] - success is a boolean to indicate if the chain is successful,
|
||||
* data is the final context after running the chain
|
||||
*/
|
||||
chain = (): Chain<InitCommandCtx> => {
|
||||
const methods = {} as Record<
|
||||
string,
|
||||
(data: Record<string, unknown>) => Chain
|
||||
>;
|
||||
const createChain = this._createChain;
|
||||
for (const [name, command] of this._commands.entries()) {
|
||||
methods[name] = function (
|
||||
this: { [cmdSymbol]: Command[] },
|
||||
data: Record<string, unknown>
|
||||
) {
|
||||
const cmds = this[cmdSymbol];
|
||||
return createChain(methods, [
|
||||
...cmds,
|
||||
(ctx, next) => command({ ...ctx, ...data }, next),
|
||||
]);
|
||||
};
|
||||
}
|
||||
|
||||
return createChain(methods, []) as never;
|
||||
};
|
||||
|
||||
/**
|
||||
* Register a command to the command manager
|
||||
* @param name
|
||||
* @param command
|
||||
* Make sure to also add the command to the global interface `BlockSuite.Commands`
|
||||
* ```ts
|
||||
* const myCommand: Command = (ctx, next) => {
|
||||
* // do something
|
||||
* }
|
||||
*
|
||||
* declare global {
|
||||
* namespace BlockSuite {
|
||||
* interface Commands {
|
||||
* 'myCommand': typeof myCommand
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* ```
|
||||
*/
|
||||
add<N extends BlockSuite.CommandName>(
|
||||
name: N,
|
||||
command: BlockSuite.Commands[N]
|
||||
): CommandManager;
|
||||
|
||||
add(name: string, command: Command) {
|
||||
this._commands.set(name, command);
|
||||
return this;
|
||||
}
|
||||
|
||||
override created() {
|
||||
const add = this.add.bind(this);
|
||||
this.std.provider.getAll(CommandIdentifier).forEach((command, key) => {
|
||||
add(key as keyof BlockSuite.Commands, command);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a registered command by name
|
||||
* @param command
|
||||
* @param payloads
|
||||
* ```ts
|
||||
* const { success, ...data } = commandManager.exec('myCommand', { data: 'data' });
|
||||
* ```
|
||||
* @returns { success, ...data } - success is a boolean to indicate if the command is successful,
|
||||
* data is the final context after running the command
|
||||
*/
|
||||
exec<K extends keyof BlockSuite.Commands>(
|
||||
command: K,
|
||||
...payloads: IfAllKeysOptional<
|
||||
Omit<InDataOfCommand<BlockSuite.Commands[K]>, keyof InitCommandCtx>,
|
||||
[
|
||||
inData: void | Omit<
|
||||
InDataOfCommand<BlockSuite.Commands[K]>,
|
||||
keyof InitCommandCtx
|
||||
>,
|
||||
],
|
||||
[
|
||||
inData: Omit<
|
||||
InDataOfCommand<BlockSuite.Commands[K]>,
|
||||
keyof InitCommandCtx
|
||||
>,
|
||||
]
|
||||
>
|
||||
): ExecCommandResult<K> & { success: boolean } {
|
||||
const cmdFunc = this._commands.get(command);
|
||||
|
||||
if (!cmdFunc) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.CommandError,
|
||||
`The command "${command}" not found`
|
||||
);
|
||||
}
|
||||
|
||||
const inData = payloads[0];
|
||||
const ctx = {
|
||||
...this._getCommandCtx(),
|
||||
...inData,
|
||||
};
|
||||
|
||||
let execResult = {
|
||||
success: false,
|
||||
} as ExecCommandResult<K> & { success: boolean };
|
||||
|
||||
cmdFunc(ctx, result => {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
execResult = { ...result, success: true };
|
||||
});
|
||||
|
||||
return execResult;
|
||||
}
|
||||
}
|
||||
|
||||
function runCmds(ctx: BlockSuite.CommandContext, [cmd, ...rest]: Command[]) {
|
||||
let _ctx = ctx;
|
||||
if (cmd) {
|
||||
cmd(ctx, data => {
|
||||
_ctx = runCmds({ ...ctx, ...data }, rest);
|
||||
});
|
||||
}
|
||||
return _ctx;
|
||||
}
|
||||
80
blocksuite/framework/block-std/src/command/types.ts
Normal file
80
blocksuite/framework/block-std/src/command/types.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
// type A = {};
|
||||
// type B = { prop?: string };
|
||||
// type C = { prop: string };
|
||||
// type TestA = MakeOptionalIfEmpty<A>; // void | {}
|
||||
// type TestB = MakeOptionalIfEmpty<B>; // void | { prop?: string }
|
||||
// type TestC = MakeOptionalIfEmpty<C>; // { prop: string }
|
||||
import type { cmdSymbol } from './consts.js';
|
||||
|
||||
export type IfAllKeysOptional<T, Yes, No> =
|
||||
Partial<T> extends T ? (T extends Partial<T> ? Yes : No) : No;
|
||||
type MakeOptionalIfEmpty<T> = IfAllKeysOptional<T, void | T, T>;
|
||||
|
||||
export interface InitCommandCtx {
|
||||
std: BlockSuite.Std;
|
||||
}
|
||||
|
||||
export type CommandKeyToData<K extends BlockSuite.CommandDataName> = Pick<
|
||||
BlockSuite.CommandContext,
|
||||
K
|
||||
>;
|
||||
export type Command<
|
||||
In extends BlockSuite.CommandDataName = never,
|
||||
Out extends BlockSuite.CommandDataName = never,
|
||||
InData extends object = {},
|
||||
> = (
|
||||
ctx: CommandKeyToData<In> & InitCommandCtx & InData,
|
||||
next: (ctx?: CommandKeyToData<Out>) => void
|
||||
) => void;
|
||||
type Omit1<A, B> = [keyof Omit<A, keyof B>] extends [never]
|
||||
? void
|
||||
: Omit<A, keyof B>;
|
||||
export type InDataOfCommand<C> =
|
||||
C extends Command<infer K, any, infer R> ? CommandKeyToData<K> & R : never;
|
||||
type OutDataOfCommand<C> =
|
||||
C extends Command<any, infer K, any> ? CommandKeyToData<K> : never;
|
||||
|
||||
type CommonMethods<In extends object = {}> = {
|
||||
inline: <InlineOut extends BlockSuite.CommandDataName = never>(
|
||||
command: Command<Extract<keyof In, BlockSuite.CommandDataName>, InlineOut>
|
||||
) => Chain<In & CommandKeyToData<InlineOut>>;
|
||||
try: <InlineOut extends BlockSuite.CommandDataName = never>(
|
||||
fn: (chain: Chain<In>) => Chain<In & CommandKeyToData<InlineOut>>[]
|
||||
) => Chain<In & CommandKeyToData<InlineOut>>;
|
||||
tryAll: <InlineOut extends BlockSuite.CommandDataName = never>(
|
||||
fn: (chain: Chain<In>) => Chain<In & CommandKeyToData<InlineOut>>[]
|
||||
) => Chain<In & CommandKeyToData<InlineOut>>;
|
||||
run(): [
|
||||
result: boolean,
|
||||
ctx: CommandKeyToData<Extract<keyof In, BlockSuite.CommandDataName>>,
|
||||
];
|
||||
with<T extends Partial<BlockSuite.CommandContext>>(value: T): Chain<In & T>;
|
||||
};
|
||||
|
||||
type Cmds = {
|
||||
[cmdSymbol]: Command[];
|
||||
};
|
||||
|
||||
export type Chain<In extends object = {}> = CommonMethods<In> & {
|
||||
[K in keyof BlockSuite.Commands]: (
|
||||
data: MakeOptionalIfEmpty<
|
||||
Omit1<InDataOfCommand<BlockSuite.Commands[K]>, In>
|
||||
>
|
||||
) => Chain<In & OutDataOfCommand<BlockSuite.Commands[K]>>;
|
||||
} & Cmds;
|
||||
|
||||
export type ExecCommandResult<K extends keyof BlockSuite.Commands> =
|
||||
OutDataOfCommand<BlockSuite.Commands[K]>;
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface CommandContext extends InitCommandCtx {}
|
||||
|
||||
interface Commands {}
|
||||
|
||||
type CommandName = keyof Commands;
|
||||
type CommandDataName = keyof CommandContext;
|
||||
|
||||
type CommandChain<In extends object = {}> = Chain<In & InitCommandCtx>;
|
||||
}
|
||||
}
|
||||
7
blocksuite/framework/block-std/src/effects.ts
Normal file
7
blocksuite/framework/block-std/src/effects.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { GfxViewportElement } from './gfx/viewport-element.js';
|
||||
import { EditorHost } from './view/index.js';
|
||||
|
||||
export function effects() {
|
||||
customElements.define('editor-host', EditorHost);
|
||||
customElements.define('gfx-viewport', GfxViewportElement);
|
||||
}
|
||||
62
blocksuite/framework/block-std/src/event/base.ts
Normal file
62
blocksuite/framework/block-std/src/event/base.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
type MatchEvent<T extends string> = T extends UIEventStateType
|
||||
? BlockSuiteUIEventState[T]
|
||||
: UIEventState;
|
||||
|
||||
export class UIEventState {
|
||||
/** when extends, override it with pattern `xxxState` */
|
||||
type = 'defaultState';
|
||||
|
||||
constructor(public event: Event) {}
|
||||
}
|
||||
|
||||
export class UIEventStateContext {
|
||||
private _map: Record<string, UIEventState> = {};
|
||||
|
||||
add = <State extends UIEventState = UIEventState>(state: State) => {
|
||||
const name = state.type;
|
||||
if (this._map[name]) {
|
||||
console.warn('UIEventStateContext: state name duplicated', name);
|
||||
}
|
||||
|
||||
this._map[name] = state;
|
||||
};
|
||||
|
||||
get = <Type extends UIEventStateType = UIEventStateType>(
|
||||
type: Type
|
||||
): MatchEvent<Type> => {
|
||||
const state = this._map[type];
|
||||
if (!state) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.EventDispatcherError,
|
||||
`UIEventStateContext: state ${type} not found`
|
||||
);
|
||||
}
|
||||
return state as MatchEvent<Type>;
|
||||
};
|
||||
|
||||
has = (type: UIEventStateType) => {
|
||||
return !!this._map[type];
|
||||
};
|
||||
|
||||
static from(...states: UIEventState[]) {
|
||||
const context = new UIEventStateContext();
|
||||
states.forEach(state => {
|
||||
context.add(state);
|
||||
});
|
||||
return context;
|
||||
}
|
||||
}
|
||||
|
||||
export type UIEventHandler = (
|
||||
context: UIEventStateContext
|
||||
) => boolean | null | undefined | void;
|
||||
|
||||
declare global {
|
||||
interface BlockSuiteUIEventState {
|
||||
defaultState: UIEventState;
|
||||
}
|
||||
|
||||
type UIEventStateType = keyof BlockSuiteUIEventState;
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { UIEventState, UIEventStateContext } from '../base.js';
|
||||
import type { UIEventDispatcher } from '../dispatcher.js';
|
||||
import { ClipboardEventState } from '../state/clipboard.js';
|
||||
import { EventScopeSourceType, EventSourceState } from '../state/source.js';
|
||||
|
||||
export class ClipboardControl {
|
||||
private _copy = (event: ClipboardEvent) => {
|
||||
const clipboardEventState = new ClipboardEventState({
|
||||
event,
|
||||
});
|
||||
this._dispatcher.run(
|
||||
'copy',
|
||||
this._createContext(event, clipboardEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private _cut = (event: ClipboardEvent) => {
|
||||
const clipboardEventState = new ClipboardEventState({
|
||||
event,
|
||||
});
|
||||
this._dispatcher.run(
|
||||
'cut',
|
||||
this._createContext(event, clipboardEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private _paste = (event: ClipboardEvent) => {
|
||||
const clipboardEventState = new ClipboardEventState({
|
||||
event,
|
||||
});
|
||||
|
||||
this._dispatcher.run(
|
||||
'paste',
|
||||
this._createContext(event, clipboardEventState)
|
||||
);
|
||||
};
|
||||
|
||||
constructor(private _dispatcher: UIEventDispatcher) {}
|
||||
|
||||
private _createContext(event: Event, clipboardState: ClipboardEventState) {
|
||||
return UIEventStateContext.from(
|
||||
new UIEventState(event),
|
||||
new EventSourceState({
|
||||
event,
|
||||
sourceType: EventScopeSourceType.Selection,
|
||||
}),
|
||||
clipboardState
|
||||
);
|
||||
}
|
||||
|
||||
listen() {
|
||||
this._dispatcher.disposables.addFromEvent(document, 'cut', this._cut);
|
||||
this._dispatcher.disposables.addFromEvent(document, 'copy', this._copy);
|
||||
this._dispatcher.disposables.addFromEvent(document, 'paste', this._paste);
|
||||
}
|
||||
}
|
||||
112
blocksuite/framework/block-std/src/event/control/keyboard.ts
Normal file
112
blocksuite/framework/block-std/src/event/control/keyboard.ts
Normal file
@@ -0,0 +1,112 @@
|
||||
import { IS_MAC } from '@blocksuite/global/env';
|
||||
|
||||
import {
|
||||
type UIEventHandler,
|
||||
UIEventState,
|
||||
UIEventStateContext,
|
||||
} from '../base.js';
|
||||
import type { EventOptions, UIEventDispatcher } from '../dispatcher.js';
|
||||
import { bindKeymap } from '../keymap.js';
|
||||
import { KeyboardEventState } from '../state/index.js';
|
||||
import { EventScopeSourceType, EventSourceState } from '../state/source.js';
|
||||
|
||||
export class KeyboardControl {
|
||||
private _down = (event: KeyboardEvent) => {
|
||||
if (!this._shouldTrigger(event)) {
|
||||
return;
|
||||
}
|
||||
const keyboardEventState = new KeyboardEventState({
|
||||
event,
|
||||
composing: this.composition,
|
||||
});
|
||||
this._dispatcher.run(
|
||||
'keyDown',
|
||||
this._createContext(event, keyboardEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private _shouldTrigger = (event: KeyboardEvent) => {
|
||||
if (event.isComposing) {
|
||||
return false;
|
||||
}
|
||||
const mod = IS_MAC ? event.metaKey : event.ctrlKey;
|
||||
if (
|
||||
['c', 'v', 'x'].includes(event.key) &&
|
||||
mod &&
|
||||
!event.shiftKey &&
|
||||
!event.altKey
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
private _up = (event: KeyboardEvent) => {
|
||||
if (!this._shouldTrigger(event)) {
|
||||
return;
|
||||
}
|
||||
const keyboardEventState = new KeyboardEventState({
|
||||
event,
|
||||
composing: this.composition,
|
||||
});
|
||||
|
||||
this._dispatcher.run(
|
||||
'keyUp',
|
||||
this._createContext(event, keyboardEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private composition = false;
|
||||
|
||||
constructor(private _dispatcher: UIEventDispatcher) {}
|
||||
|
||||
private _createContext(event: Event, keyboardState: KeyboardEventState) {
|
||||
return UIEventStateContext.from(
|
||||
new UIEventState(event),
|
||||
new EventSourceState({
|
||||
event,
|
||||
sourceType: EventScopeSourceType.Selection,
|
||||
}),
|
||||
keyboardState
|
||||
);
|
||||
}
|
||||
|
||||
bindHotkey(keymap: Record<string, UIEventHandler>, options?: EventOptions) {
|
||||
return this._dispatcher.add(
|
||||
'keyDown',
|
||||
ctx => {
|
||||
if (this.composition) {
|
||||
return false;
|
||||
}
|
||||
const binding = bindKeymap(keymap);
|
||||
return binding(ctx);
|
||||
},
|
||||
options
|
||||
);
|
||||
}
|
||||
|
||||
listen() {
|
||||
this._dispatcher.disposables.addFromEvent(document, 'keydown', this._down);
|
||||
this._dispatcher.disposables.addFromEvent(document, 'keyup', this._up);
|
||||
this._dispatcher.disposables.addFromEvent(
|
||||
document,
|
||||
'compositionstart',
|
||||
() => {
|
||||
this.composition = true;
|
||||
},
|
||||
{
|
||||
capture: true,
|
||||
}
|
||||
);
|
||||
this._dispatcher.disposables.addFromEvent(
|
||||
document,
|
||||
'compositionend',
|
||||
() => {
|
||||
this.composition = false;
|
||||
},
|
||||
{
|
||||
capture: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
594
blocksuite/framework/block-std/src/event/control/pointer.ts
Normal file
594
blocksuite/framework/block-std/src/event/control/pointer.ts
Normal file
@@ -0,0 +1,594 @@
|
||||
import { IS_IPAD } from '@blocksuite/global/env';
|
||||
import { nextTick, Vec } from '@blocksuite/global/utils';
|
||||
|
||||
import { UIEventState, UIEventStateContext } from '../base.js';
|
||||
import type { UIEventDispatcher } from '../dispatcher.js';
|
||||
import {
|
||||
DndEventState,
|
||||
MultiPointerEventState,
|
||||
PointerEventState,
|
||||
} from '../state/index.js';
|
||||
import { EventScopeSourceType, EventSourceState } from '../state/source.js';
|
||||
import { isFarEnough } from '../utils.js';
|
||||
|
||||
type PointerId = typeof PointerEvent.prototype.pointerId;
|
||||
|
||||
function createContext(
|
||||
event: Event,
|
||||
state: PointerEventState | MultiPointerEventState
|
||||
) {
|
||||
return UIEventStateContext.from(
|
||||
new UIEventState(event),
|
||||
new EventSourceState({
|
||||
event,
|
||||
sourceType: EventScopeSourceType.Target,
|
||||
}),
|
||||
state
|
||||
);
|
||||
}
|
||||
|
||||
const POLL_INTERVAL = 1000;
|
||||
|
||||
abstract class PointerControllerBase {
|
||||
constructor(
|
||||
protected _dispatcher: UIEventDispatcher,
|
||||
protected _getRect: () => DOMRect
|
||||
) {}
|
||||
|
||||
abstract listen(): void;
|
||||
}
|
||||
|
||||
class PointerEventForward extends PointerControllerBase {
|
||||
private _down = (event: PointerEvent) => {
|
||||
const { pointerId } = event;
|
||||
|
||||
const pointerState = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: -Infinity,
|
||||
startY: -Infinity,
|
||||
last: null,
|
||||
});
|
||||
this._startStates.set(pointerId, pointerState);
|
||||
this._lastStates.set(pointerId, pointerState);
|
||||
this._dispatcher.run('pointerDown', createContext(event, pointerState));
|
||||
};
|
||||
|
||||
private _lastStates = new Map<PointerId, PointerEventState>();
|
||||
|
||||
private _move = (event: PointerEvent) => {
|
||||
const { pointerId } = event;
|
||||
|
||||
const start = this._startStates.get(pointerId) ?? null;
|
||||
const last = this._lastStates.get(pointerId) ?? null;
|
||||
|
||||
const state = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: start?.x ?? -Infinity,
|
||||
startY: start?.y ?? -Infinity,
|
||||
last,
|
||||
});
|
||||
this._lastStates.set(pointerId, state);
|
||||
|
||||
this._dispatcher.run('pointerMove', createContext(event, state));
|
||||
};
|
||||
|
||||
private _startStates = new Map<PointerId, PointerEventState>();
|
||||
|
||||
private _upOrOut = (up: boolean) => (event: PointerEvent) => {
|
||||
const { pointerId } = event;
|
||||
|
||||
const start = this._startStates.get(pointerId) ?? null;
|
||||
const last = this._lastStates.get(pointerId) ?? null;
|
||||
|
||||
const state = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: start?.x ?? -Infinity,
|
||||
startY: start?.y ?? -Infinity,
|
||||
last,
|
||||
});
|
||||
|
||||
this._startStates.delete(pointerId);
|
||||
this._lastStates.delete(pointerId);
|
||||
|
||||
this._dispatcher.run(
|
||||
up ? 'pointerUp' : 'pointerOut',
|
||||
createContext(event, state)
|
||||
);
|
||||
};
|
||||
|
||||
listen() {
|
||||
const { host, disposables } = this._dispatcher;
|
||||
disposables.addFromEvent(host, 'pointerdown', this._down);
|
||||
disposables.addFromEvent(host, 'pointermove', this._move);
|
||||
disposables.addFromEvent(host, 'pointerup', this._upOrOut(true));
|
||||
disposables.addFromEvent(host, 'pointerout', this._upOrOut(false));
|
||||
}
|
||||
}
|
||||
|
||||
class ClickController extends PointerControllerBase {
|
||||
private _down = (event: PointerEvent) => {
|
||||
// disable for secondary pointer
|
||||
if (event.isPrimary === false) return;
|
||||
|
||||
if (
|
||||
this._downPointerState &&
|
||||
event.pointerId === this._downPointerState.raw.pointerId &&
|
||||
event.timeStamp - this._downPointerState.raw.timeStamp < 500 &&
|
||||
!isFarEnough(event, this._downPointerState.raw)
|
||||
) {
|
||||
this._pointerDownCount++;
|
||||
} else {
|
||||
this._pointerDownCount = 1;
|
||||
}
|
||||
|
||||
this._downPointerState = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: -Infinity,
|
||||
startY: -Infinity,
|
||||
last: null,
|
||||
});
|
||||
};
|
||||
|
||||
private _downPointerState: PointerEventState | null = null;
|
||||
|
||||
private _pointerDownCount = 0;
|
||||
|
||||
private _up = (event: PointerEvent) => {
|
||||
if (!this._downPointerState) return;
|
||||
|
||||
if (isFarEnough(this._downPointerState.raw, event)) {
|
||||
this._pointerDownCount = 0;
|
||||
this._downPointerState = null;
|
||||
return;
|
||||
}
|
||||
|
||||
const state = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: -Infinity,
|
||||
startY: -Infinity,
|
||||
last: null,
|
||||
});
|
||||
const context = createContext(event, state);
|
||||
|
||||
const run = () => {
|
||||
this._dispatcher.run('click', context);
|
||||
if (this._pointerDownCount === 2) {
|
||||
this._dispatcher.run('doubleClick', context);
|
||||
}
|
||||
if (this._pointerDownCount === 3) {
|
||||
this._dispatcher.run('tripleClick', context);
|
||||
}
|
||||
};
|
||||
|
||||
run();
|
||||
};
|
||||
|
||||
listen() {
|
||||
const { host, disposables } = this._dispatcher;
|
||||
|
||||
disposables.addFromEvent(host, 'pointerdown', this._down);
|
||||
disposables.addFromEvent(host, 'pointerup', this._up);
|
||||
}
|
||||
}
|
||||
|
||||
class DragController extends PointerControllerBase {
|
||||
private _down = (event: PointerEvent) => {
|
||||
if (this._nativeDragging) return;
|
||||
|
||||
if (!event.isPrimary) {
|
||||
if (this._dragging && this._lastPointerState) {
|
||||
this._up(this._lastPointerState.raw);
|
||||
}
|
||||
this._reset();
|
||||
return;
|
||||
}
|
||||
|
||||
const pointerState = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: -Infinity,
|
||||
startY: -Infinity,
|
||||
last: null,
|
||||
});
|
||||
this._startPointerState = pointerState;
|
||||
|
||||
this._dispatcher.disposables.addFromEvent(
|
||||
document,
|
||||
'pointermove',
|
||||
this._move
|
||||
);
|
||||
this._dispatcher.disposables.addFromEvent(document, 'pointerup', this._up);
|
||||
};
|
||||
|
||||
private _dragging = false;
|
||||
|
||||
private _lastPointerState: PointerEventState | null = null;
|
||||
|
||||
private _move = (event: PointerEvent) => {
|
||||
if (
|
||||
this._startPointerState === null ||
|
||||
this._startPointerState.raw.pointerId !== event.pointerId
|
||||
)
|
||||
return;
|
||||
|
||||
const start = this._startPointerState;
|
||||
const last = this._lastPointerState ?? start;
|
||||
|
||||
const state = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: start.x,
|
||||
startY: start.y,
|
||||
last,
|
||||
});
|
||||
|
||||
this._lastPointerState = state;
|
||||
|
||||
if (
|
||||
!this._nativeDragging &&
|
||||
!this._dragging &&
|
||||
isFarEnough(event, this._startPointerState.raw)
|
||||
) {
|
||||
this._dragging = true;
|
||||
this._dispatcher.run('dragStart', createContext(event, start));
|
||||
}
|
||||
|
||||
if (this._dragging) {
|
||||
this._dispatcher.run('dragMove', createContext(event, state));
|
||||
}
|
||||
};
|
||||
|
||||
private _nativeDragEnd = (event: DragEvent) => {
|
||||
this._nativeDragging = false;
|
||||
const dndEventState = new DndEventState({ event });
|
||||
this._dispatcher.run(
|
||||
'nativeDragEnd',
|
||||
this._createContext(event, dndEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private _nativeDragging = false;
|
||||
|
||||
private _nativeDragMove = (event: DragEvent) => {
|
||||
const dndEventState = new DndEventState({ event });
|
||||
this._dispatcher.run(
|
||||
'nativeDragMove',
|
||||
this._createContext(event, dndEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private _nativeDragStart = (event: DragEvent) => {
|
||||
this._reset();
|
||||
this._nativeDragging = true;
|
||||
const dndEventState = new DndEventState({ event });
|
||||
this._dispatcher.run(
|
||||
'nativeDragStart',
|
||||
this._createContext(event, dndEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private _nativeDrop = (event: DragEvent) => {
|
||||
this._reset();
|
||||
this._nativeDragging = false;
|
||||
const dndEventState = new DndEventState({ event });
|
||||
this._dispatcher.run(
|
||||
'nativeDrop',
|
||||
this._createContext(event, dndEventState)
|
||||
);
|
||||
};
|
||||
|
||||
private _reset = () => {
|
||||
this._dragging = false;
|
||||
this._startPointerState = null;
|
||||
this._lastPointerState = null;
|
||||
|
||||
document.removeEventListener('pointermove', this._move);
|
||||
document.removeEventListener('pointerup', this._up);
|
||||
};
|
||||
|
||||
private _startPointerState: PointerEventState | null = null;
|
||||
|
||||
private _up = (event: PointerEvent) => {
|
||||
if (
|
||||
!this._startPointerState ||
|
||||
this._startPointerState.raw.pointerId !== event.pointerId
|
||||
)
|
||||
return;
|
||||
|
||||
const start = this._startPointerState;
|
||||
const last = this._lastPointerState;
|
||||
|
||||
const state = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: start.x,
|
||||
startY: start.y,
|
||||
last,
|
||||
});
|
||||
|
||||
if (this._dragging) {
|
||||
this._dispatcher.run('dragEnd', createContext(event, state));
|
||||
}
|
||||
|
||||
this._reset();
|
||||
};
|
||||
|
||||
// https://mikepk.com/2020/10/iOS-safari-scribble-bug/
|
||||
private _applyScribblePatch() {
|
||||
if (!IS_IPAD) return;
|
||||
|
||||
const { host, disposables } = this._dispatcher;
|
||||
disposables.addFromEvent(host, 'touchmove', (event: TouchEvent) => {
|
||||
if (
|
||||
this._dragging &&
|
||||
this._startPointerState &&
|
||||
this._startPointerState.raw.pointerType === 'pen'
|
||||
) {
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private _createContext(event: Event, dndState: DndEventState) {
|
||||
return UIEventStateContext.from(
|
||||
new UIEventState(event),
|
||||
new EventSourceState({
|
||||
event,
|
||||
sourceType: EventScopeSourceType.Target,
|
||||
}),
|
||||
dndState
|
||||
);
|
||||
}
|
||||
|
||||
listen() {
|
||||
const { host, disposables } = this._dispatcher;
|
||||
disposables.addFromEvent(host, 'pointerdown', this._down);
|
||||
this._applyScribblePatch();
|
||||
|
||||
disposables.addFromEvent(host, 'dragstart', this._nativeDragStart);
|
||||
disposables.addFromEvent(host, 'dragend', this._nativeDragEnd);
|
||||
disposables.addFromEvent(host, 'drag', this._nativeDragMove);
|
||||
disposables.addFromEvent(host, 'drop', this._nativeDrop);
|
||||
}
|
||||
}
|
||||
|
||||
abstract class DualDragControllerBase extends PointerControllerBase {
|
||||
private _down = (event: PointerEvent) => {
|
||||
// Another pointer down
|
||||
if (
|
||||
this._startPointerStates.primary !== null &&
|
||||
this._startPointerStates.secondary !== null
|
||||
) {
|
||||
this._reset();
|
||||
}
|
||||
|
||||
if (this._startPointerStates.primary === null && !event.isPrimary) {
|
||||
return;
|
||||
}
|
||||
|
||||
const state = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: -Infinity,
|
||||
startY: -Infinity,
|
||||
last: null,
|
||||
});
|
||||
|
||||
if (event.isPrimary) {
|
||||
this._startPointerStates.primary = state;
|
||||
} else {
|
||||
this._startPointerStates.secondary = state;
|
||||
}
|
||||
};
|
||||
|
||||
private _lastPointerStates: {
|
||||
primary: PointerEventState | null;
|
||||
secondary: PointerEventState | null;
|
||||
} = {
|
||||
primary: null,
|
||||
secondary: null,
|
||||
};
|
||||
|
||||
private _move = (event: PointerEvent) => {
|
||||
if (
|
||||
this._startPointerStates.primary === null ||
|
||||
this._startPointerStates.secondary === null
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { isPrimary } = event;
|
||||
const startPrimaryState = this._startPointerStates.primary;
|
||||
let lastPrimaryState = this._lastPointerStates.primary;
|
||||
|
||||
const startSecondaryState = this._startPointerStates.secondary;
|
||||
let lastSecondaryState = this._lastPointerStates.secondary;
|
||||
|
||||
if (isPrimary) {
|
||||
lastPrimaryState = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: startPrimaryState.x,
|
||||
startY: startPrimaryState.y,
|
||||
last: lastPrimaryState,
|
||||
});
|
||||
} else {
|
||||
lastSecondaryState = new PointerEventState({
|
||||
event,
|
||||
rect: this._getRect(),
|
||||
startX: startSecondaryState.x,
|
||||
startY: startSecondaryState.y,
|
||||
last: lastSecondaryState,
|
||||
});
|
||||
}
|
||||
|
||||
const multiPointerState = new MultiPointerEventState(event, [
|
||||
lastPrimaryState ?? startPrimaryState,
|
||||
lastSecondaryState ?? startSecondaryState,
|
||||
]);
|
||||
|
||||
this._handleMove(event, multiPointerState);
|
||||
|
||||
this._lastPointerStates = {
|
||||
primary: lastPrimaryState,
|
||||
secondary: lastSecondaryState,
|
||||
};
|
||||
};
|
||||
|
||||
private _reset = () => {
|
||||
this._startPointerStates = {
|
||||
primary: null,
|
||||
secondary: null,
|
||||
};
|
||||
this._lastPointerStates = {
|
||||
primary: null,
|
||||
secondary: null,
|
||||
};
|
||||
};
|
||||
|
||||
private _startPointerStates: {
|
||||
primary: PointerEventState | null;
|
||||
secondary: PointerEventState | null;
|
||||
} = {
|
||||
primary: null,
|
||||
secondary: null,
|
||||
};
|
||||
|
||||
private _upOrOut = (event: PointerEvent) => {
|
||||
const { pointerId } = event;
|
||||
if (
|
||||
pointerId === this._startPointerStates.primary?.raw.pointerId ||
|
||||
pointerId === this._startPointerStates.secondary?.raw.pointerId
|
||||
) {
|
||||
this._reset();
|
||||
}
|
||||
};
|
||||
|
||||
abstract _handleMove(
|
||||
event: PointerEvent,
|
||||
state: MultiPointerEventState
|
||||
): void;
|
||||
|
||||
override listen(): void {
|
||||
const { host, disposables } = this._dispatcher;
|
||||
disposables.addFromEvent(host, 'pointerdown', this._down);
|
||||
disposables.addFromEvent(host, 'pointermove', this._move);
|
||||
disposables.addFromEvent(host, 'pointerup', this._upOrOut);
|
||||
disposables.addFromEvent(host, 'pointerout', this._upOrOut);
|
||||
}
|
||||
}
|
||||
|
||||
class PinchController extends DualDragControllerBase {
|
||||
override _handleMove(event: PointerEvent, state: MultiPointerEventState) {
|
||||
if (event.pointerType !== 'touch') return;
|
||||
|
||||
const deltaFirstPointer = state.pointers[0].delta;
|
||||
const deltaSecondPointer = state.pointers[1].delta;
|
||||
|
||||
const deltaFirstPointerVec = Vec.toVec(deltaFirstPointer);
|
||||
const deltaSecondPointerVec = Vec.toVec(deltaSecondPointer);
|
||||
|
||||
const deltaFirstPointerValue = Vec.len(deltaFirstPointerVec);
|
||||
const deltaSecondPointerValue = Vec.len(deltaSecondPointerVec);
|
||||
|
||||
const deltaDotProduct = Vec.dpr(
|
||||
deltaFirstPointerVec,
|
||||
deltaSecondPointerVec
|
||||
);
|
||||
|
||||
const deltaValueThreshold = 0.1;
|
||||
|
||||
// the changes of distance between two pointers is not far enough
|
||||
if (
|
||||
!isFarEnough(deltaFirstPointer, deltaSecondPointer) ||
|
||||
deltaDotProduct > 0 ||
|
||||
deltaFirstPointerValue < deltaValueThreshold ||
|
||||
deltaSecondPointerValue < deltaValueThreshold
|
||||
)
|
||||
return;
|
||||
|
||||
this._dispatcher.run('pinch', createContext(event, state));
|
||||
}
|
||||
}
|
||||
|
||||
class PanController extends DualDragControllerBase {
|
||||
override _handleMove(event: PointerEvent, state: MultiPointerEventState) {
|
||||
if (event.pointerType !== 'touch') return;
|
||||
|
||||
const deltaFirstPointer = state.pointers[0].delta;
|
||||
const deltaSecondPointer = state.pointers[1].delta;
|
||||
|
||||
const deltaDotProduct = Vec.dpr(
|
||||
Vec.toVec(deltaFirstPointer),
|
||||
Vec.toVec(deltaSecondPointer)
|
||||
);
|
||||
|
||||
// the center move distance is not far enough
|
||||
if (
|
||||
!isFarEnough(deltaFirstPointer, deltaSecondPointer) &&
|
||||
deltaDotProduct < 0
|
||||
)
|
||||
return;
|
||||
|
||||
this._dispatcher.run('pan', createContext(event, state));
|
||||
}
|
||||
}
|
||||
|
||||
export class PointerControl {
|
||||
private _cachedRect: DOMRect | null = null;
|
||||
|
||||
private _getRect = () => {
|
||||
if (this._cachedRect === null) {
|
||||
this._updateRect();
|
||||
}
|
||||
return this._cachedRect as DOMRect;
|
||||
};
|
||||
|
||||
// XXX: polling is used instead of MutationObserver
|
||||
// due to potential performance issues
|
||||
private _pollingInterval: number | null = null;
|
||||
|
||||
private controllers: PointerControllerBase[];
|
||||
|
||||
constructor(private _dispatcher: UIEventDispatcher) {
|
||||
this.controllers = [
|
||||
new PointerEventForward(_dispatcher, this._getRect),
|
||||
new ClickController(_dispatcher, this._getRect),
|
||||
new DragController(_dispatcher, this._getRect),
|
||||
new PanController(_dispatcher, this._getRect),
|
||||
new PinchController(_dispatcher, this._getRect),
|
||||
];
|
||||
}
|
||||
|
||||
private _startPolling() {
|
||||
const poll = () => {
|
||||
nextTick()
|
||||
.then(() => this._updateRect())
|
||||
.catch(console.error);
|
||||
};
|
||||
this._pollingInterval = window.setInterval(poll, POLL_INTERVAL);
|
||||
poll();
|
||||
}
|
||||
|
||||
protected _updateRect() {
|
||||
if (!this._dispatcher.host) return;
|
||||
this._cachedRect = this._dispatcher.host.getBoundingClientRect();
|
||||
}
|
||||
|
||||
dispose() {
|
||||
if (this._pollingInterval !== null) {
|
||||
clearInterval(this._pollingInterval);
|
||||
this._pollingInterval = null;
|
||||
}
|
||||
}
|
||||
|
||||
listen() {
|
||||
this._startPolling();
|
||||
this.controllers.forEach(controller => controller.listen());
|
||||
}
|
||||
}
|
||||
156
blocksuite/framework/block-std/src/event/control/range.ts
Normal file
156
blocksuite/framework/block-std/src/event/control/range.ts
Normal file
@@ -0,0 +1,156 @@
|
||||
import type { BlockComponent } from '../../view/index.js';
|
||||
import { UIEventState, UIEventStateContext } from '../base.js';
|
||||
import type {
|
||||
EventHandlerRunner,
|
||||
EventName,
|
||||
UIEventDispatcher,
|
||||
} from '../dispatcher.js';
|
||||
import { EventScopeSourceType, EventSourceState } from '../state/source.js';
|
||||
|
||||
export class RangeControl {
|
||||
private _buildScope = (eventName: EventName) => {
|
||||
let scope: EventHandlerRunner[] | undefined;
|
||||
const selection = document.getSelection();
|
||||
if (selection && selection.rangeCount > 0) {
|
||||
const range = selection.getRangeAt(0);
|
||||
scope = this._buildEventScopeByNativeRange(eventName, range);
|
||||
this._prev = range;
|
||||
} else if (this._prev !== null) {
|
||||
scope = this._buildEventScopeByNativeRange(eventName, this._prev);
|
||||
this._prev = null;
|
||||
}
|
||||
|
||||
return scope;
|
||||
};
|
||||
|
||||
private _compositionEnd = (event: Event) => {
|
||||
const scope = this._buildScope('compositionEnd');
|
||||
|
||||
this._dispatcher.run('compositionEnd', this._createContext(event), scope);
|
||||
};
|
||||
|
||||
private _compositionStart = (event: Event) => {
|
||||
const scope = this._buildScope('compositionStart');
|
||||
|
||||
this._dispatcher.run('compositionStart', this._createContext(event), scope);
|
||||
};
|
||||
|
||||
private _compositionUpdate = (event: Event) => {
|
||||
const scope = this._buildScope('compositionUpdate');
|
||||
|
||||
this._dispatcher.run(
|
||||
'compositionUpdate',
|
||||
this._createContext(event),
|
||||
scope
|
||||
);
|
||||
};
|
||||
|
||||
private _prev: Range | null = null;
|
||||
|
||||
private _selectionChange = (event: Event) => {
|
||||
const selection = document.getSelection();
|
||||
if (!selection) return;
|
||||
|
||||
if (!selection.containsNode(this._dispatcher.host, true)) return;
|
||||
if (selection.containsNode(this._dispatcher.host)) return;
|
||||
|
||||
const scope = this._buildScope('selectionChange');
|
||||
|
||||
this._dispatcher.run('selectionChange', this._createContext(event), scope);
|
||||
};
|
||||
|
||||
constructor(private _dispatcher: UIEventDispatcher) {}
|
||||
|
||||
private _buildEventScopeByNativeRange(name: EventName, range: Range) {
|
||||
const blockIds = this._findBlockComponentPath(range);
|
||||
|
||||
return this._dispatcher.buildEventScope(name, blockIds);
|
||||
}
|
||||
|
||||
private _createContext(event: Event) {
|
||||
return UIEventStateContext.from(
|
||||
new UIEventState(event),
|
||||
new EventSourceState({
|
||||
event,
|
||||
sourceType: EventScopeSourceType.Selection,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private _findBlockComponentPath(range: Range): string[] {
|
||||
const start = range.startContainer;
|
||||
const end = range.endContainer;
|
||||
const ancestor = range.commonAncestorContainer;
|
||||
const getBlockView = (node: Node): BlockComponent | null => {
|
||||
const el = node instanceof Element ? node : node.parentElement;
|
||||
// TODO(mirone/#6534): find a better way to get block element from a node
|
||||
return el?.closest<BlockComponent>('[data-block-id]') ?? null;
|
||||
};
|
||||
if (ancestor.nodeType === Node.TEXT_NODE) {
|
||||
const leaf = getBlockView(ancestor);
|
||||
if (leaf) {
|
||||
return [leaf.blockId];
|
||||
}
|
||||
}
|
||||
const nodes = new Set<Node>();
|
||||
|
||||
let startRecorded = false;
|
||||
const dfsDOMSearch = (current: Node | null, ancestor: Node) => {
|
||||
if (!current) {
|
||||
return;
|
||||
}
|
||||
if (current === ancestor) {
|
||||
return;
|
||||
}
|
||||
if (current === end) {
|
||||
nodes.add(current);
|
||||
startRecorded = false;
|
||||
return;
|
||||
}
|
||||
if (current === start) {
|
||||
startRecorded = true;
|
||||
}
|
||||
// eslint-disable-next-line sonarjs/no-collapsible-if
|
||||
if (startRecorded) {
|
||||
if (
|
||||
current.nodeType === Node.TEXT_NODE ||
|
||||
current.nodeType === Node.ELEMENT_NODE
|
||||
) {
|
||||
nodes.add(current);
|
||||
}
|
||||
}
|
||||
dfsDOMSearch(current.firstChild, ancestor);
|
||||
dfsDOMSearch(current.nextSibling, ancestor);
|
||||
};
|
||||
dfsDOMSearch(ancestor.firstChild, ancestor);
|
||||
|
||||
const blocks = new Set<string>();
|
||||
nodes.forEach(node => {
|
||||
const blockView = getBlockView(node);
|
||||
if (!blockView) {
|
||||
return;
|
||||
}
|
||||
if (blocks.has(blockView.blockId)) {
|
||||
return;
|
||||
}
|
||||
blocks.add(blockView.blockId);
|
||||
});
|
||||
return Array.from(blocks);
|
||||
}
|
||||
|
||||
listen() {
|
||||
const { host, disposables } = this._dispatcher;
|
||||
disposables.addFromEvent(
|
||||
document,
|
||||
'selectionchange',
|
||||
this._selectionChange
|
||||
);
|
||||
disposables.addFromEvent(host, 'compositionstart', this._compositionStart);
|
||||
disposables.addFromEvent(host, 'compositionend', this._compositionEnd);
|
||||
disposables.addFromEvent(
|
||||
host,
|
||||
'compositionupdate',
|
||||
this._compositionUpdate
|
||||
);
|
||||
}
|
||||
}
|
||||
405
blocksuite/framework/block-std/src/event/dispatcher.ts
Normal file
405
blocksuite/framework/block-std/src/event/dispatcher.ts
Normal file
@@ -0,0 +1,405 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { DisposableGroup } from '@blocksuite/global/utils';
|
||||
|
||||
import { LifeCycleWatcher } from '../extension/index.js';
|
||||
import { KeymapIdentifier } from '../identifier.js';
|
||||
import { type BlockComponent, EditorHost } from '../view/index.js';
|
||||
import {
|
||||
type UIEventHandler,
|
||||
UIEventState,
|
||||
UIEventStateContext,
|
||||
} from './base.js';
|
||||
import { ClipboardControl } from './control/clipboard.js';
|
||||
import { KeyboardControl } from './control/keyboard.js';
|
||||
import { PointerControl } from './control/pointer.js';
|
||||
import { RangeControl } from './control/range.js';
|
||||
import { EventScopeSourceType, EventSourceState } from './state/source.js';
|
||||
import { toLowerCase } from './utils.js';
|
||||
|
||||
const bypassEventNames = [
|
||||
'beforeInput',
|
||||
|
||||
'blur',
|
||||
'focus',
|
||||
'contextMenu',
|
||||
'wheel',
|
||||
] as const;
|
||||
|
||||
const eventNames = [
|
||||
'click',
|
||||
'doubleClick',
|
||||
'tripleClick',
|
||||
|
||||
'pointerDown',
|
||||
'pointerMove',
|
||||
'pointerUp',
|
||||
'pointerOut',
|
||||
|
||||
'dragStart',
|
||||
'dragMove',
|
||||
'dragEnd',
|
||||
|
||||
'pinch',
|
||||
'pan',
|
||||
|
||||
'keyDown',
|
||||
'keyUp',
|
||||
|
||||
'selectionChange',
|
||||
'compositionStart',
|
||||
'compositionUpdate',
|
||||
'compositionEnd',
|
||||
|
||||
'cut',
|
||||
'copy',
|
||||
'paste',
|
||||
|
||||
'nativeDragStart',
|
||||
'nativeDragMove',
|
||||
'nativeDragEnd',
|
||||
'nativeDrop',
|
||||
|
||||
...bypassEventNames,
|
||||
] as const;
|
||||
|
||||
export type EventName = (typeof eventNames)[number];
|
||||
export type EventOptions = {
|
||||
flavour?: string;
|
||||
blockId?: string;
|
||||
};
|
||||
export type EventHandlerRunner = {
|
||||
fn: UIEventHandler;
|
||||
flavour?: string;
|
||||
blockId?: string;
|
||||
};
|
||||
|
||||
export class UIEventDispatcher extends LifeCycleWatcher {
|
||||
private static _activeDispatcher: UIEventDispatcher | null = null;
|
||||
|
||||
static override readonly key = 'UIEventDispatcher';
|
||||
|
||||
private _active = false;
|
||||
|
||||
private _clipboardControl: ClipboardControl;
|
||||
|
||||
private _handlersMap = Object.fromEntries(
|
||||
eventNames.map((name): [EventName, Array<EventHandlerRunner>] => [name, []])
|
||||
) as Record<EventName, Array<EventHandlerRunner>>;
|
||||
|
||||
private _keyboardControl: KeyboardControl;
|
||||
|
||||
private _pointerControl: PointerControl;
|
||||
|
||||
private _rangeControl: RangeControl;
|
||||
|
||||
bindHotkey = (...args: Parameters<KeyboardControl['bindHotkey']>) =>
|
||||
this._keyboardControl.bindHotkey(...args);
|
||||
|
||||
disposables = new DisposableGroup();
|
||||
|
||||
private get _currentSelections() {
|
||||
return this.std.selection.value;
|
||||
}
|
||||
|
||||
get active() {
|
||||
return this._active;
|
||||
}
|
||||
|
||||
get host() {
|
||||
return this.std.host;
|
||||
}
|
||||
|
||||
constructor(std: BlockSuite.Std) {
|
||||
super(std);
|
||||
this._pointerControl = new PointerControl(this);
|
||||
this._keyboardControl = new KeyboardControl(this);
|
||||
this._rangeControl = new RangeControl(this);
|
||||
this._clipboardControl = new ClipboardControl(this);
|
||||
this.disposables.add(this._pointerControl);
|
||||
}
|
||||
|
||||
private _bindEvents() {
|
||||
bypassEventNames.forEach(eventName => {
|
||||
this.disposables.addFromEvent(
|
||||
this.host,
|
||||
toLowerCase(eventName),
|
||||
event => {
|
||||
this.run(
|
||||
eventName,
|
||||
UIEventStateContext.from(
|
||||
new UIEventState(event),
|
||||
new EventSourceState({
|
||||
event,
|
||||
sourceType: EventScopeSourceType.Selection,
|
||||
})
|
||||
)
|
||||
);
|
||||
},
|
||||
eventName === 'wheel'
|
||||
? {
|
||||
passive: false,
|
||||
}
|
||||
: undefined
|
||||
);
|
||||
});
|
||||
|
||||
this._pointerControl.listen();
|
||||
this._keyboardControl.listen();
|
||||
this._rangeControl.listen();
|
||||
this._clipboardControl.listen();
|
||||
|
||||
let _dragging = false;
|
||||
this.disposables.addFromEvent(this.host, 'pointerdown', () => {
|
||||
_dragging = true;
|
||||
this._setActive(true);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'pointerup', () => {
|
||||
_dragging = false;
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'click', () => {
|
||||
this._setActive(true);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'focusin', () => {
|
||||
this._setActive(true);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'focusout', e => {
|
||||
if (e.relatedTarget && !this.host.contains(e.relatedTarget as Node)) {
|
||||
this._setActive(false);
|
||||
}
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'blur', () => {
|
||||
if (_dragging) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._setActive(false);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'dragover', () => {
|
||||
this._setActive(true);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'dragend', () => {
|
||||
this._setActive(false);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'drop', () => {
|
||||
this._setActive(true);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'pointerenter', () => {
|
||||
if (this._isActiveElementOutsideHost()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._setActive(true);
|
||||
});
|
||||
this.disposables.addFromEvent(this.host, 'pointerleave', () => {
|
||||
if (
|
||||
(document.activeElement &&
|
||||
this.host.contains(document.activeElement)) ||
|
||||
_dragging
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._setActive(false);
|
||||
});
|
||||
}
|
||||
|
||||
private _buildEventScopeBySelection(name: EventName) {
|
||||
const handlers = this._handlersMap[name];
|
||||
if (!handlers) return;
|
||||
|
||||
const selections = this._currentSelections;
|
||||
const ids = selections.map(selection => selection.blockId);
|
||||
|
||||
return this.buildEventScope(name, ids);
|
||||
}
|
||||
|
||||
private _buildEventScopeByTarget(name: EventName, target: Node) {
|
||||
const handlers = this._handlersMap[name];
|
||||
if (!handlers) return;
|
||||
|
||||
// TODO(mirone/#6534): find a better way to get block element from a node
|
||||
const el = target instanceof Element ? target : target.parentElement;
|
||||
const block = el?.closest<BlockComponent>('[data-block-id]');
|
||||
|
||||
const blockId = block?.blockId;
|
||||
if (!blockId) {
|
||||
return this._buildEventScopeBySelection(name);
|
||||
}
|
||||
|
||||
return this.buildEventScope(name, [blockId]);
|
||||
}
|
||||
|
||||
private _getDeepActiveElement(): Element | null {
|
||||
let active = document.activeElement;
|
||||
while (active && active.shadowRoot && active.shadowRoot.activeElement) {
|
||||
active = active.shadowRoot.activeElement;
|
||||
}
|
||||
return active;
|
||||
}
|
||||
|
||||
private _getEventScope(name: EventName, state: EventSourceState) {
|
||||
const handlers = this._handlersMap[name];
|
||||
if (!handlers) return;
|
||||
|
||||
let output: EventHandlerRunner[] | undefined;
|
||||
|
||||
switch (state.sourceType) {
|
||||
case EventScopeSourceType.Selection: {
|
||||
output = this._buildEventScopeBySelection(name);
|
||||
break;
|
||||
}
|
||||
case EventScopeSourceType.Target: {
|
||||
output = this._buildEventScopeByTarget(
|
||||
name,
|
||||
state.event.target as Node
|
||||
);
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.EventDispatcherError,
|
||||
`Unknown event scope source: ${state.sourceType}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
private _isActiveElementOutsideHost(): boolean {
|
||||
const activeElement = this._getDeepActiveElement();
|
||||
return (
|
||||
activeElement !== null &&
|
||||
this._isEditableElementActive(activeElement) &&
|
||||
!this.host.contains(activeElement)
|
||||
);
|
||||
}
|
||||
|
||||
private _isEditableElementActive(element: Element | null): boolean {
|
||||
if (!element) return false;
|
||||
return (
|
||||
element instanceof HTMLInputElement ||
|
||||
element instanceof HTMLTextAreaElement ||
|
||||
(element instanceof EditorHost && !element.doc.readonly) ||
|
||||
(element as HTMLElement).isContentEditable
|
||||
);
|
||||
}
|
||||
|
||||
private _setActive(active: boolean) {
|
||||
if (active) {
|
||||
if (UIEventDispatcher._activeDispatcher !== this) {
|
||||
if (UIEventDispatcher._activeDispatcher) {
|
||||
UIEventDispatcher._activeDispatcher._active = false;
|
||||
}
|
||||
UIEventDispatcher._activeDispatcher = this;
|
||||
}
|
||||
this._active = true;
|
||||
} else {
|
||||
if (UIEventDispatcher._activeDispatcher === this) {
|
||||
UIEventDispatcher._activeDispatcher = null;
|
||||
}
|
||||
this._active = false;
|
||||
}
|
||||
}
|
||||
|
||||
add(name: EventName, handler: UIEventHandler, options?: EventOptions) {
|
||||
const runner: EventHandlerRunner = {
|
||||
fn: handler,
|
||||
flavour: options?.flavour,
|
||||
blockId: options?.blockId,
|
||||
};
|
||||
this._handlersMap[name].unshift(runner);
|
||||
return () => {
|
||||
if (this._handlersMap[name].includes(runner)) {
|
||||
this._handlersMap[name] = this._handlersMap[name].filter(
|
||||
x => x !== runner
|
||||
);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
buildEventScope(
|
||||
name: EventName,
|
||||
blocks: string[]
|
||||
): EventHandlerRunner[] | undefined {
|
||||
const handlers = this._handlersMap[name];
|
||||
if (!handlers) return;
|
||||
|
||||
const globalEvents = handlers.filter(
|
||||
handler => handler.flavour === undefined && handler.blockId === undefined
|
||||
);
|
||||
|
||||
let blockIds: string[] = blocks;
|
||||
const events: EventHandlerRunner[] = [];
|
||||
const flavourSeen: Record<string, boolean> = {};
|
||||
while (blockIds.length > 0) {
|
||||
const idHandlers = handlers.filter(
|
||||
handler => handler.blockId && blockIds.includes(handler.blockId)
|
||||
);
|
||||
|
||||
const flavourHandlers = blockIds
|
||||
.map(blockId => this.std.doc.getBlock(blockId)?.flavour)
|
||||
.filter((flavour): flavour is string => {
|
||||
if (!flavour) return false;
|
||||
if (flavourSeen[flavour]) return false;
|
||||
flavourSeen[flavour] = true;
|
||||
return true;
|
||||
})
|
||||
.flatMap(flavour => {
|
||||
return handlers.filter(handler => handler.flavour === flavour);
|
||||
});
|
||||
|
||||
events.push(...idHandlers, ...flavourHandlers);
|
||||
blockIds = blockIds
|
||||
.map(blockId => {
|
||||
const parent = this.std.doc.getParent(blockId);
|
||||
return parent?.id;
|
||||
})
|
||||
.filter((id): id is string => !!id);
|
||||
}
|
||||
|
||||
return events.concat(globalEvents);
|
||||
}
|
||||
|
||||
override mounted() {
|
||||
if (this.disposables.disposed) {
|
||||
this.disposables = new DisposableGroup();
|
||||
}
|
||||
this._bindEvents();
|
||||
|
||||
const std = this.std;
|
||||
this.std.provider
|
||||
.getAll(KeymapIdentifier)
|
||||
.forEach(({ getter, options }) => {
|
||||
this.bindHotkey(getter(std), options);
|
||||
});
|
||||
}
|
||||
|
||||
run(
|
||||
name: EventName,
|
||||
context: UIEventStateContext,
|
||||
runners?: EventHandlerRunner[]
|
||||
) {
|
||||
if (!this.active) return;
|
||||
|
||||
const sourceState = context.get('sourceState');
|
||||
if (!runners) {
|
||||
runners = this._getEventScope(name, sourceState);
|
||||
if (!runners) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (const runner of runners) {
|
||||
const { fn } = runner;
|
||||
const result = fn(context);
|
||||
if (result) {
|
||||
context.get('defaultState').event.stopPropagation();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override unmounted() {
|
||||
this.disposables.dispose();
|
||||
}
|
||||
}
|
||||
4
blocksuite/framework/block-std/src/event/index.ts
Normal file
4
blocksuite/framework/block-std/src/event/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './base.js';
|
||||
export * from './dispatcher.js';
|
||||
export * from './keymap.js';
|
||||
export * from './state/index.js';
|
||||
109
blocksuite/framework/block-std/src/event/keymap.ts
Normal file
109
blocksuite/framework/block-std/src/event/keymap.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { base, keyName } from 'w3c-keyname';
|
||||
|
||||
import type { UIEventHandler } from './base.js';
|
||||
|
||||
const mac =
|
||||
typeof navigator !== 'undefined'
|
||||
? /Mac|iP(hone|[oa]d)/.test(navigator.platform)
|
||||
: false;
|
||||
|
||||
function normalizeKeyName(name: string) {
|
||||
const parts = name.split(/-(?!$)/);
|
||||
let result = parts.at(-1);
|
||||
if (result === 'Space') {
|
||||
result = ' ';
|
||||
}
|
||||
let alt, ctrl, shift, meta;
|
||||
parts.slice(0, -1).forEach(mod => {
|
||||
if (/^(cmd|meta|m)$/i.test(mod)) {
|
||||
meta = true;
|
||||
return;
|
||||
}
|
||||
if (/^a(lt)?$/i.test(mod)) {
|
||||
alt = true;
|
||||
return;
|
||||
}
|
||||
if (/^(c|ctrl|control)$/i.test(mod)) {
|
||||
ctrl = true;
|
||||
return;
|
||||
}
|
||||
if (/^s(hift)?$/i.test(mod)) {
|
||||
shift = true;
|
||||
return;
|
||||
}
|
||||
if (/^mod$/i.test(mod)) {
|
||||
if (mac) {
|
||||
meta = true;
|
||||
} else {
|
||||
ctrl = true;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.EventDispatcherError,
|
||||
'Unrecognized modifier name: ' + mod
|
||||
);
|
||||
});
|
||||
if (alt) result = 'Alt-' + result;
|
||||
if (ctrl) result = 'Ctrl-' + result;
|
||||
if (meta) result = 'Meta-' + result;
|
||||
if (shift) result = 'Shift-' + result;
|
||||
return result as string;
|
||||
}
|
||||
|
||||
function modifiers(name: string, event: KeyboardEvent, shift = true) {
|
||||
if (event.altKey) name = 'Alt-' + name;
|
||||
if (event.ctrlKey) name = 'Ctrl-' + name;
|
||||
if (event.metaKey) name = 'Meta-' + name;
|
||||
if (shift && event.shiftKey) name = 'Shift-' + name;
|
||||
return name;
|
||||
}
|
||||
|
||||
function normalize(map: Record<string, UIEventHandler>) {
|
||||
const copy: Record<string, UIEventHandler> = Object.create(null);
|
||||
for (const prop in map) copy[normalizeKeyName(prop)] = map[prop];
|
||||
return copy;
|
||||
}
|
||||
|
||||
export function bindKeymap(
|
||||
bindings: Record<string, UIEventHandler>
|
||||
): UIEventHandler {
|
||||
const map = normalize(bindings);
|
||||
return ctx => {
|
||||
const state = ctx.get('keyboardState');
|
||||
const event = state.raw;
|
||||
const name = keyName(event);
|
||||
const direct = map[modifiers(name, event)];
|
||||
if (direct && direct(ctx)) {
|
||||
return true;
|
||||
}
|
||||
if (name.length !== 1 || name === ' ') {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (event.shiftKey) {
|
||||
const noShift = map[modifiers(name, event, false)];
|
||||
if (noShift && noShift(ctx)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// none standard keyboard, fallback to keyCode
|
||||
const special =
|
||||
event.shiftKey ||
|
||||
event.altKey ||
|
||||
event.metaKey ||
|
||||
name.charCodeAt(0) > 127;
|
||||
const baseName = base[event.keyCode];
|
||||
if (special && baseName && baseName !== name) {
|
||||
const fromCode = map[modifiers(baseName, event)];
|
||||
if (fromCode && fromCode(ctx)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
}
|
||||
23
blocksuite/framework/block-std/src/event/state/clipboard.ts
Normal file
23
blocksuite/framework/block-std/src/event/state/clipboard.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { UIEventState } from '../base.js';
|
||||
|
||||
type ClipboardEventStateOptions = {
|
||||
event: ClipboardEvent;
|
||||
};
|
||||
|
||||
export class ClipboardEventState extends UIEventState {
|
||||
raw: ClipboardEvent;
|
||||
|
||||
override type = 'clipboardState';
|
||||
|
||||
constructor({ event }: ClipboardEventStateOptions) {
|
||||
super(event);
|
||||
|
||||
this.raw = event;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface BlockSuiteUIEventState {
|
||||
clipboardState: ClipboardEventState;
|
||||
}
|
||||
}
|
||||
23
blocksuite/framework/block-std/src/event/state/dnd.ts
Normal file
23
blocksuite/framework/block-std/src/event/state/dnd.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { UIEventState } from '../base.js';
|
||||
|
||||
type DndEventStateOptions = {
|
||||
event: DragEvent;
|
||||
};
|
||||
|
||||
export class DndEventState extends UIEventState {
|
||||
raw: DragEvent;
|
||||
|
||||
override type = 'dndState';
|
||||
|
||||
constructor({ event }: DndEventStateOptions) {
|
||||
super(event);
|
||||
|
||||
this.raw = event;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface BlockSuiteUIEventState {
|
||||
dndState: DndEventState;
|
||||
}
|
||||
}
|
||||
5
blocksuite/framework/block-std/src/event/state/index.ts
Normal file
5
blocksuite/framework/block-std/src/event/state/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from './clipboard.js';
|
||||
export * from './dnd.js';
|
||||
export * from './keyboard.js';
|
||||
export * from './pointer.js';
|
||||
export * from './source.js';
|
||||
27
blocksuite/framework/block-std/src/event/state/keyboard.ts
Normal file
27
blocksuite/framework/block-std/src/event/state/keyboard.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { UIEventState } from '../base.js';
|
||||
|
||||
type KeyboardEventStateOptions = {
|
||||
event: KeyboardEvent;
|
||||
composing: boolean;
|
||||
};
|
||||
|
||||
export class KeyboardEventState extends UIEventState {
|
||||
composing: boolean;
|
||||
|
||||
raw: KeyboardEvent;
|
||||
|
||||
override type = 'keyboardState';
|
||||
|
||||
constructor({ event, composing }: KeyboardEventStateOptions) {
|
||||
super(event);
|
||||
|
||||
this.raw = event;
|
||||
this.composing = composing;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface BlockSuiteUIEventState {
|
||||
keyboardState: KeyboardEventState;
|
||||
}
|
||||
}
|
||||
83
blocksuite/framework/block-std/src/event/state/pointer.ts
Normal file
83
blocksuite/framework/block-std/src/event/state/pointer.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { UIEventState } from '../base.js';
|
||||
|
||||
type PointerEventStateOptions = {
|
||||
event: PointerEvent;
|
||||
rect: DOMRect;
|
||||
startX: number;
|
||||
startY: number;
|
||||
last: PointerEventState | null;
|
||||
};
|
||||
|
||||
type Point = { x: number; y: number };
|
||||
|
||||
export class PointerEventState extends UIEventState {
|
||||
button: number;
|
||||
|
||||
containerOffset: Point;
|
||||
|
||||
delta: Point;
|
||||
|
||||
keys: {
|
||||
shift: boolean;
|
||||
cmd: boolean;
|
||||
alt: boolean;
|
||||
};
|
||||
|
||||
point: Point;
|
||||
|
||||
pressure: number;
|
||||
|
||||
raw: PointerEvent;
|
||||
|
||||
start: Point;
|
||||
|
||||
override type = 'pointerState';
|
||||
|
||||
get x() {
|
||||
return this.point.x;
|
||||
}
|
||||
|
||||
get y() {
|
||||
return this.point.y;
|
||||
}
|
||||
|
||||
constructor({ event, rect, startX, startY, last }: PointerEventStateOptions) {
|
||||
super(event);
|
||||
|
||||
const offsetX = event.clientX - rect.left;
|
||||
const offsetY = event.clientY - rect.top;
|
||||
|
||||
this.raw = event;
|
||||
this.point = { x: offsetX, y: offsetY };
|
||||
this.containerOffset = { x: rect.left, y: rect.top };
|
||||
this.start = { x: startX, y: startY };
|
||||
this.delta = last
|
||||
? { x: offsetX - last.point.x, y: offsetY - last.point.y }
|
||||
: { x: 0, y: 0 };
|
||||
this.keys = {
|
||||
shift: event.shiftKey,
|
||||
cmd: event.metaKey || event.ctrlKey,
|
||||
alt: event.altKey,
|
||||
};
|
||||
this.button = last?.button || event.button;
|
||||
this.pressure = event.pressure;
|
||||
}
|
||||
}
|
||||
|
||||
export class MultiPointerEventState extends UIEventState {
|
||||
pointers: PointerEventState[];
|
||||
|
||||
override type = 'multiPointerState';
|
||||
|
||||
constructor(event: PointerEvent, pointers: PointerEventState[]) {
|
||||
super(event);
|
||||
this.pointers = pointers;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface BlockSuiteUIEventState {
|
||||
pointerState: PointerEventState;
|
||||
multiPointerState: MultiPointerEventState;
|
||||
}
|
||||
}
|
||||
31
blocksuite/framework/block-std/src/event/state/source.ts
Normal file
31
blocksuite/framework/block-std/src/event/state/source.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { UIEventState } from '../base.js';
|
||||
|
||||
export enum EventScopeSourceType {
|
||||
// The event scope should be built by selection path
|
||||
Selection = 'selection',
|
||||
|
||||
// The event scope should be built by event target
|
||||
Target = 'target',
|
||||
}
|
||||
|
||||
export type EventSourceStateOptions = {
|
||||
event: Event;
|
||||
sourceType: EventScopeSourceType;
|
||||
};
|
||||
|
||||
export class EventSourceState extends UIEventState {
|
||||
readonly sourceType: EventScopeSourceType;
|
||||
|
||||
override type = 'sourceState';
|
||||
|
||||
constructor({ event, sourceType }: EventSourceStateOptions) {
|
||||
super(event);
|
||||
this.sourceType = sourceType;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
interface BlockSuiteUIEventState {
|
||||
sourceState: EventSourceState;
|
||||
}
|
||||
}
|
||||
17
blocksuite/framework/block-std/src/event/utils.ts
Normal file
17
blocksuite/framework/block-std/src/event/utils.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { IPoint } from '@blocksuite/global/utils';
|
||||
|
||||
export function isFarEnough(a: IPoint, b: IPoint) {
|
||||
const dx = a.x - b.x;
|
||||
const dy = a.y - b.y;
|
||||
return Math.pow(dx, 2) + Math.pow(dy, 2) > 4;
|
||||
}
|
||||
|
||||
export function center(a: IPoint, b: IPoint) {
|
||||
return {
|
||||
x: (a.x + b.x) / 2,
|
||||
y: (a.y + b.y) / 2,
|
||||
};
|
||||
}
|
||||
|
||||
export const toLowerCase = <T extends string>(str: T): Lowercase<T> =>
|
||||
str.toLowerCase() as Lowercase<T>;
|
||||
32
blocksuite/framework/block-std/src/extension/block-view.ts
Normal file
32
blocksuite/framework/block-std/src/extension/block-view.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { BlockViewIdentifier } from '../identifier.js';
|
||||
import type { BlockViewType } from '../spec/type.js';
|
||||
import type { ExtensionType } from './extension.js';
|
||||
|
||||
/**
|
||||
* Create a block view extension.
|
||||
*
|
||||
* @param flavour The flavour of the block that the view is for.
|
||||
* @param view Lit literal template for the view. Example: `my-list-block`
|
||||
*
|
||||
* The view is a lit template that is used to render the block.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { BlockViewExtension } from '@blocksuite/block-std';
|
||||
*
|
||||
* const MyListBlockViewExtension = BlockViewExtension(
|
||||
* 'affine:list',
|
||||
* literal`my-list-block`
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
export function BlockViewExtension(
|
||||
flavour: BlockSuite.Flavour,
|
||||
view: BlockViewType
|
||||
): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(BlockViewIdentifier(flavour), () => view);
|
||||
},
|
||||
};
|
||||
}
|
||||
27
blocksuite/framework/block-std/src/extension/command.ts
Normal file
27
blocksuite/framework/block-std/src/extension/command.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { CommandIdentifier } from '../identifier.js';
|
||||
import type { BlockCommands } from '../spec/index.js';
|
||||
import type { ExtensionType } from './extension.js';
|
||||
|
||||
/**
|
||||
* Create a command extension.
|
||||
*
|
||||
* @param commands A map of command names to command implementations.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { CommandExtension } from '@blocksuite/block-std';
|
||||
*
|
||||
* const MyCommandExtension = CommandExtension({
|
||||
* 'my-command': MyCommand
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export function CommandExtension(commands: BlockCommands): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
Object.entries(commands).forEach(([name, command]) => {
|
||||
di.addImpl(CommandIdentifier(name), () => command);
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
30
blocksuite/framework/block-std/src/extension/config.ts
Normal file
30
blocksuite/framework/block-std/src/extension/config.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { ConfigIdentifier } from '../identifier.js';
|
||||
import type { ExtensionType } from './extension.js';
|
||||
|
||||
/**
|
||||
* Create a config extension.
|
||||
* A config extension provides a configuration object for a block flavour.
|
||||
* The configuration object can be used like:
|
||||
* ```ts
|
||||
* const config = std.provider.get(ConfigIdentifier('my-flavour'));
|
||||
* ```
|
||||
*
|
||||
* @param flavor The flavour of the block that the config is for.
|
||||
* @param config The configuration object.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { ConfigExtension } from '@blocksuite/block-std';
|
||||
* const MyConfigExtension = ConfigExtension('my-flavour', config);
|
||||
* ```
|
||||
*/
|
||||
export function ConfigExtension(
|
||||
flavor: BlockSuite.Flavour,
|
||||
config: Record<string, unknown>
|
||||
): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(ConfigIdentifier(flavor), () => config);
|
||||
},
|
||||
};
|
||||
}
|
||||
17
blocksuite/framework/block-std/src/extension/extension.ts
Normal file
17
blocksuite/framework/block-std/src/extension/extension.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import type { Container } from '@blocksuite/global/di';
|
||||
|
||||
/**
|
||||
* Generic extension.
|
||||
* Extensions are used to set up the dependency injection container.
|
||||
* In most cases, you won't need to use this class directly.
|
||||
* We provide helper classes like `CommandExtension` and `BlockViewExtension` to make it easier to create extensions.
|
||||
*/
|
||||
export abstract class Extension {
|
||||
static setup(_di: Container): void {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
export interface ExtensionType {
|
||||
setup(di: Container): void;
|
||||
}
|
||||
25
blocksuite/framework/block-std/src/extension/flavour.ts
Normal file
25
blocksuite/framework/block-std/src/extension/flavour.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { BlockFlavourIdentifier } from '../identifier.js';
|
||||
import type { ExtensionType } from './extension.js';
|
||||
|
||||
/**
|
||||
* Create a flavour extension.
|
||||
*
|
||||
* @param flavour
|
||||
* The flavour of the block that the extension is for.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { FlavourExtension } from '@blocksuite/block-std';
|
||||
*
|
||||
* const MyFlavourExtension = FlavourExtension('my-flavour');
|
||||
* ```
|
||||
*/
|
||||
export function FlavourExtension(flavour: string): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(BlockFlavourIdentifier(flavour), () => ({
|
||||
flavour,
|
||||
}));
|
||||
},
|
||||
};
|
||||
}
|
||||
11
blocksuite/framework/block-std/src/extension/index.ts
Normal file
11
blocksuite/framework/block-std/src/extension/index.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export * from './block-view.js';
|
||||
export * from './command.js';
|
||||
export * from './config.js';
|
||||
export * from './extension.js';
|
||||
export * from './flavour.js';
|
||||
export * from './keymap.js';
|
||||
export * from './lifecycle-watcher.js';
|
||||
export * from './selection.js';
|
||||
export * from './service.js';
|
||||
export * from './service-watcher.js';
|
||||
export * from './widget-view-map.js';
|
||||
48
blocksuite/framework/block-std/src/extension/keymap.ts
Normal file
48
blocksuite/framework/block-std/src/extension/keymap.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import type { EventOptions, UIEventHandler } from '../event/index.js';
|
||||
import { KeymapIdentifier } from '../identifier.js';
|
||||
import type { BlockStdScope } from '../scope/index.js';
|
||||
import type { ExtensionType } from './extension.js';
|
||||
|
||||
let id = 1;
|
||||
|
||||
/**
|
||||
* Create a keymap extension.
|
||||
*
|
||||
* @param keymapFactory
|
||||
* Create keymap of the extension.
|
||||
* It should return an object with `keymap` and `options`.
|
||||
*
|
||||
* `keymap` is a record of keymap.
|
||||
*
|
||||
* @param options
|
||||
* `options` is an optional object that restricts the event to be handled.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { KeymapExtension } from '@blocksuite/block-std';
|
||||
*
|
||||
* const MyKeymapExtension = KeymapExtension(std => {
|
||||
* return {
|
||||
* keymap: {
|
||||
* 'mod-a': SelectAll
|
||||
* }
|
||||
* options: {
|
||||
* flavour: 'affine:paragraph'
|
||||
* }
|
||||
* }
|
||||
* });
|
||||
* ```
|
||||
*/
|
||||
export function KeymapExtension(
|
||||
keymapFactory: (std: BlockStdScope) => Record<string, UIEventHandler>,
|
||||
options?: EventOptions
|
||||
): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(KeymapIdentifier(`Keymap-${id++}`), {
|
||||
getter: keymapFactory,
|
||||
options,
|
||||
});
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import type { Container } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
import { LifeCycleWatcherIdentifier, StdIdentifier } from '../identifier.js';
|
||||
import type { BlockStdScope } from '../scope/index.js';
|
||||
import { Extension } from './extension.js';
|
||||
|
||||
/**
|
||||
* A life cycle watcher is an extension that watches the life cycle of the editor.
|
||||
* It is used to perform actions when the editor is created, mounted, rendered, or unmounted.
|
||||
*
|
||||
* When creating a life cycle watcher, you must define a key that is unique to the watcher.
|
||||
* The key is used to identify the watcher in the dependency injection container.
|
||||
* ```ts
|
||||
* class MyLifeCycleWatcher extends LifeCycleWatcher {
|
||||
* static override readonly key = 'my-life-cycle-watcher';
|
||||
* ```
|
||||
*
|
||||
* In the life cycle watcher, the methods will be called in the following order:
|
||||
* 1. `created`: Called when the std is created.
|
||||
* 2. `rendered`: Called when `std.render` is called.
|
||||
* 3. `mounted`: Called when the editor host is mounted.
|
||||
* 4. `unmounted`: Called when the editor host is unmounted.
|
||||
*/
|
||||
export abstract class LifeCycleWatcher extends Extension {
|
||||
static key: string;
|
||||
|
||||
constructor(readonly std: BlockStdScope) {
|
||||
super();
|
||||
}
|
||||
|
||||
static override setup(di: Container) {
|
||||
if (!this.key) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'Key is not defined in the LifeCycleWatcher'
|
||||
);
|
||||
}
|
||||
|
||||
di.add(this as unknown as { new (std: BlockStdScope): LifeCycleWatcher }, [
|
||||
StdIdentifier,
|
||||
]);
|
||||
|
||||
di.addImpl(LifeCycleWatcherIdentifier(this.key), provider =>
|
||||
provider.get(this)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when std is created.
|
||||
*/
|
||||
created() {}
|
||||
|
||||
/**
|
||||
* Called when editor host is mounted.
|
||||
* Which means the editor host emit the `connectedCallback` lifecycle event.
|
||||
*/
|
||||
mounted() {}
|
||||
|
||||
/**
|
||||
* Called when `std.render` is called.
|
||||
*/
|
||||
rendered() {}
|
||||
|
||||
/**
|
||||
* Called when editor host is unmounted.
|
||||
* Which means the editor host emit the `disconnectedCallback` lifecycle event.
|
||||
*/
|
||||
unmounted() {}
|
||||
}
|
||||
13
blocksuite/framework/block-std/src/extension/selection.ts
Normal file
13
blocksuite/framework/block-std/src/extension/selection.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { SelectionIdentifier } from '../identifier.js';
|
||||
import type { SelectionConstructor } from '../selection/index.js';
|
||||
import type { ExtensionType } from './extension.js';
|
||||
|
||||
export function SelectionExtension(
|
||||
selectionCtor: SelectionConstructor
|
||||
): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(SelectionIdentifier(selectionCtor.type), () => selectionCtor);
|
||||
},
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
import type { Container } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
import {
|
||||
BlockServiceIdentifier,
|
||||
LifeCycleWatcherIdentifier,
|
||||
StdIdentifier,
|
||||
} from '../identifier.js';
|
||||
import type { BlockStdScope } from '../scope/index.js';
|
||||
import { LifeCycleWatcher } from './lifecycle-watcher.js';
|
||||
import type { BlockService } from './service.js';
|
||||
|
||||
const idMap = new Map<string, number>();
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* BlockServiceWatcher is deprecated. You should reconsider where to put your feature.
|
||||
*
|
||||
* BlockServiceWatcher is a legacy extension that is used to watch the slots registered on block service.
|
||||
* However, we recommend using the new extension system.
|
||||
*/
|
||||
export abstract class BlockServiceWatcher extends LifeCycleWatcher {
|
||||
static flavour: string;
|
||||
|
||||
constructor(
|
||||
std: BlockStdScope,
|
||||
readonly blockService: BlockService
|
||||
) {
|
||||
super(std);
|
||||
}
|
||||
|
||||
static override setup(di: Container) {
|
||||
if (!this.flavour) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'Flavour is not defined in the BlockServiceWatcher'
|
||||
);
|
||||
}
|
||||
const id = idMap.get(this.flavour) ?? 0;
|
||||
idMap.set(this.flavour, id + 1);
|
||||
di.addImpl(
|
||||
LifeCycleWatcherIdentifier(`${this.flavour}-watcher-${id}`),
|
||||
this,
|
||||
[StdIdentifier, BlockServiceIdentifier(this.flavour)]
|
||||
);
|
||||
}
|
||||
}
|
||||
120
blocksuite/framework/block-std/src/extension/service.ts
Normal file
120
blocksuite/framework/block-std/src/extension/service.ts
Normal file
@@ -0,0 +1,120 @@
|
||||
import type { Container } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { DisposableGroup } from '@blocksuite/global/utils';
|
||||
|
||||
import type { EventName, UIEventHandler } from '../event/index.js';
|
||||
import {
|
||||
BlockFlavourIdentifier,
|
||||
BlockServiceIdentifier,
|
||||
StdIdentifier,
|
||||
} from '../identifier.js';
|
||||
import type { BlockStdScope } from '../scope/index.js';
|
||||
import { getSlots } from '../spec/index.js';
|
||||
import { Extension } from './extension.js';
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* BlockService is deprecated. You should reconsider where to put your feature.
|
||||
*
|
||||
* BlockService is a legacy extension that is used to provide services to the block.
|
||||
* In the previous version of BlockSuite, block service provides a way to extend the block.
|
||||
* However, in the new version, we recommend using the new extension system.
|
||||
*/
|
||||
export abstract class BlockService extends Extension {
|
||||
static flavour: string;
|
||||
|
||||
readonly disposables = new DisposableGroup();
|
||||
|
||||
readonly flavour: string;
|
||||
|
||||
readonly specSlots = getSlots();
|
||||
|
||||
get collection() {
|
||||
return this.std.collection;
|
||||
}
|
||||
|
||||
get doc() {
|
||||
return this.std.doc;
|
||||
}
|
||||
|
||||
get host() {
|
||||
return this.std.host;
|
||||
}
|
||||
|
||||
get selectionManager() {
|
||||
return this.std.selection;
|
||||
}
|
||||
|
||||
get uiEventDispatcher() {
|
||||
return this.std.event;
|
||||
}
|
||||
|
||||
constructor(
|
||||
readonly std: BlockStdScope,
|
||||
readonly flavourProvider: { flavour: string }
|
||||
) {
|
||||
super();
|
||||
this.flavour = flavourProvider.flavour;
|
||||
}
|
||||
|
||||
static override setup(di: Container) {
|
||||
if (!this.flavour) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'Flavour is not defined in the BlockService'
|
||||
);
|
||||
}
|
||||
di.add(
|
||||
this as unknown as {
|
||||
new (
|
||||
std: BlockStdScope,
|
||||
flavourProvider: { flavour: string }
|
||||
): BlockService;
|
||||
},
|
||||
[StdIdentifier, BlockFlavourIdentifier(this.flavour)]
|
||||
);
|
||||
di.addImpl(BlockServiceIdentifier(this.flavour), provider =>
|
||||
provider.get(this)
|
||||
);
|
||||
}
|
||||
|
||||
bindHotKey(
|
||||
keymap: Record<string, UIEventHandler>,
|
||||
options?: { global: boolean }
|
||||
) {
|
||||
this.disposables.add(
|
||||
this.uiEventDispatcher.bindHotkey(keymap, {
|
||||
flavour: options?.global ? undefined : this.flavour,
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
// life cycle start
|
||||
dispose() {
|
||||
this.disposables.dispose();
|
||||
}
|
||||
|
||||
// event handlers start
|
||||
handleEvent(
|
||||
name: EventName,
|
||||
fn: UIEventHandler,
|
||||
options?: { global: boolean }
|
||||
) {
|
||||
this.disposables.add(
|
||||
this.uiEventDispatcher.add(name, fn, {
|
||||
flavour: options?.global ? undefined : this.flavour,
|
||||
})
|
||||
);
|
||||
}
|
||||
// life cycle end
|
||||
|
||||
mounted() {
|
||||
this.specSlots.mounted.emit({ service: this });
|
||||
}
|
||||
|
||||
unmounted() {
|
||||
this.dispose();
|
||||
this.specSlots.unmounted.emit({ service: this });
|
||||
}
|
||||
// event handlers end
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
import { WidgetViewMapIdentifier } from '../identifier.js';
|
||||
import type { WidgetViewMapType } from '../spec/type.js';
|
||||
import type { ExtensionType } from './extension.js';
|
||||
|
||||
/**
|
||||
* Create a widget view map extension.
|
||||
*
|
||||
* @param flavour The flavour of the block that the widget view map is for.
|
||||
* @param widgetViewMap A map of widget names to widget view lit literal.
|
||||
*
|
||||
* A widget view map is to provide a map of widgets to a block.
|
||||
* For every target block, it's view will be rendered with the widget views.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { WidgetViewMapExtension } from '@blocksuite/block-std';
|
||||
*
|
||||
* const MyWidgetViewMapExtension = WidgetViewMapExtension('my-flavour', {
|
||||
* 'my-widget': literal`my-widget-view`
|
||||
* });
|
||||
*/
|
||||
export function WidgetViewMapExtension(
|
||||
flavour: BlockSuite.Flavour,
|
||||
widgetViewMap: WidgetViewMapType
|
||||
): ExtensionType {
|
||||
return {
|
||||
setup: di => {
|
||||
di.addImpl(WidgetViewMapIdentifier(flavour), () => widgetViewMap);
|
||||
},
|
||||
};
|
||||
}
|
||||
303
blocksuite/framework/block-std/src/gfx/controller.ts
Normal file
303
blocksuite/framework/block-std/src/gfx/controller.ts
Normal file
@@ -0,0 +1,303 @@
|
||||
import {
|
||||
assertType,
|
||||
Bound,
|
||||
DisposableGroup,
|
||||
getCommonBoundWithRotation,
|
||||
type IBound,
|
||||
last,
|
||||
} from '@blocksuite/global/utils';
|
||||
import type { BlockModel } from '@blocksuite/store';
|
||||
import { Signal } from '@preact/signals-core';
|
||||
|
||||
import { LifeCycleWatcher } from '../extension/lifecycle-watcher.js';
|
||||
import type { BlockStdScope } from '../scope/block-std-scope.js';
|
||||
import { onSurfaceAdded } from '../utils/gfx.js';
|
||||
import type { BlockComponent } from '../view/index.js';
|
||||
import type { CursorType } from './cursor.js';
|
||||
import {
|
||||
GfxClassExtenderIdentifier,
|
||||
GfxExtensionIdentifier,
|
||||
} from './extension.js';
|
||||
import { GridManager } from './grid.js';
|
||||
import { gfxControllerKey } from './identifiers.js';
|
||||
import { KeyboardController } from './keyboard.js';
|
||||
import { LayerManager } from './layer.js';
|
||||
import type { PointTestOptions } from './model/base.js';
|
||||
import { GfxBlockElementModel } from './model/gfx-block-model.js';
|
||||
import type { GfxModel } from './model/model.js';
|
||||
import {
|
||||
GfxGroupLikeElementModel,
|
||||
GfxPrimitiveElementModel,
|
||||
} from './model/surface/element-model.js';
|
||||
import type { SurfaceBlockModel } from './model/surface/surface-model.js';
|
||||
import { Viewport } from './viewport.js';
|
||||
|
||||
export class GfxController extends LifeCycleWatcher {
|
||||
static override key = gfxControllerKey;
|
||||
|
||||
private _disposables: DisposableGroup = new DisposableGroup();
|
||||
|
||||
private _surface: SurfaceBlockModel | null = null;
|
||||
|
||||
readonly cursor$ = new Signal<CursorType>();
|
||||
|
||||
readonly grid: GridManager;
|
||||
|
||||
readonly keyboard: KeyboardController;
|
||||
|
||||
readonly layer: LayerManager;
|
||||
|
||||
readonly viewport: Viewport = new Viewport();
|
||||
|
||||
get doc() {
|
||||
return this.std.doc;
|
||||
}
|
||||
|
||||
get elementsBound() {
|
||||
return getCommonBoundWithRotation(this.gfxElements);
|
||||
}
|
||||
|
||||
get gfxElements(): GfxModel[] {
|
||||
return [...this.layer.blocks, ...this.layer.canvasElements];
|
||||
}
|
||||
|
||||
get surface() {
|
||||
return this._surface;
|
||||
}
|
||||
|
||||
get surfaceComponent(): BlockComponent | null {
|
||||
return this.surface
|
||||
? (this.std.view.getBlock(this.surface.id) ?? null)
|
||||
: null;
|
||||
}
|
||||
|
||||
constructor(std: BlockStdScope) {
|
||||
super(std);
|
||||
|
||||
this.grid = new GridManager();
|
||||
this.layer = new LayerManager(this.doc, null);
|
||||
this.keyboard = new KeyboardController(std);
|
||||
|
||||
this._disposables.add(
|
||||
onSurfaceAdded(this.doc, surface => {
|
||||
this._surface = surface;
|
||||
|
||||
if (surface) {
|
||||
this._disposables.add(this.grid.watch({ surface }));
|
||||
this.layer.watch({ surface });
|
||||
}
|
||||
})
|
||||
);
|
||||
this._disposables.add(this.grid.watch({ doc: this.doc }));
|
||||
this._disposables.add(this.layer);
|
||||
this._disposables.add(this.viewport);
|
||||
this._disposables.add(this.keyboard);
|
||||
|
||||
this.std.provider.getAll(GfxClassExtenderIdentifier).forEach(ext => {
|
||||
ext.extendFn(this);
|
||||
});
|
||||
}
|
||||
|
||||
deleteElement(element: GfxModel | BlockModel<object> | string): void {
|
||||
element = typeof element === 'string' ? element : element.id;
|
||||
|
||||
assertType<string>(element);
|
||||
|
||||
if (this.surface?.hasElementById(element)) {
|
||||
this.surface.deleteElement(element);
|
||||
} else {
|
||||
const block = this.doc.getBlock(element)?.model;
|
||||
block && this.doc.deleteBlock(block);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a block or element by its id.
|
||||
* Note that non-gfx block can also be queried in this method.
|
||||
* @param id
|
||||
* @returns
|
||||
*/
|
||||
getElementById<
|
||||
T extends GfxModel | BlockModel<object> = GfxModel | BlockModel<object>,
|
||||
>(id: string): T | null {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
return (
|
||||
this.surface?.getElementById(id) ?? this.doc.getBlock(id)?.model ?? null
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get elements on a specific point.
|
||||
* @param x
|
||||
* @param y
|
||||
* @param options
|
||||
*/
|
||||
getElementByPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
options: { all: true } & PointTestOptions
|
||||
): GfxModel[];
|
||||
getElementByPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
options?: { all?: false } & PointTestOptions
|
||||
): GfxModel | null;
|
||||
getElementByPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
options: PointTestOptions & {
|
||||
all?: boolean;
|
||||
} = { all: false, hitThreshold: 10 }
|
||||
): GfxModel | GfxModel[] | null {
|
||||
options.zoom = this.viewport.zoom;
|
||||
options.hitThreshold ??= 10;
|
||||
|
||||
const hitThreshold = options.hitThreshold;
|
||||
const responsePadding = options.responsePadding ?? [
|
||||
hitThreshold / 2,
|
||||
hitThreshold / 2,
|
||||
];
|
||||
const all = options.all ?? false;
|
||||
const hitTestBound = {
|
||||
x: x - responsePadding[0],
|
||||
y: y - responsePadding[1],
|
||||
w: responsePadding[0] * 2,
|
||||
h: responsePadding[1] * 2,
|
||||
};
|
||||
|
||||
const candidates = this.grid.search(hitTestBound);
|
||||
const picked = candidates.filter(
|
||||
elm =>
|
||||
elm.includesPoint(x, y, options as PointTestOptions, this.std.host) ||
|
||||
elm.externalBound?.isPointInBound([x, y])
|
||||
);
|
||||
|
||||
picked.sort(this.layer.compare);
|
||||
|
||||
if (all) {
|
||||
return picked;
|
||||
}
|
||||
|
||||
return last(picked) ?? null;
|
||||
}
|
||||
|
||||
getElementInGroup(
|
||||
x: number,
|
||||
y: number,
|
||||
options?: PointTestOptions
|
||||
): GfxModel | null {
|
||||
const selectionManager = this.selection;
|
||||
const results = this.getElementByPoint(x, y, {
|
||||
...options,
|
||||
all: true,
|
||||
});
|
||||
|
||||
let picked = last(results) ?? null;
|
||||
const { activeGroup } = selectionManager;
|
||||
const first = picked;
|
||||
|
||||
if (activeGroup && picked && activeGroup.hasDescendant(picked)) {
|
||||
let index = results.length - 1;
|
||||
|
||||
while (
|
||||
picked === activeGroup ||
|
||||
(picked instanceof GfxGroupLikeElementModel &&
|
||||
picked.hasDescendant(activeGroup))
|
||||
) {
|
||||
picked = results[--index];
|
||||
}
|
||||
} else if (picked) {
|
||||
let index = results.length - 1;
|
||||
|
||||
while (picked.group instanceof GfxGroupLikeElementModel) {
|
||||
if (--index < 0) {
|
||||
picked = null;
|
||||
break;
|
||||
}
|
||||
picked = results[index];
|
||||
}
|
||||
}
|
||||
|
||||
return (picked ?? first) as GfxModel | null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Query all elements in an area.
|
||||
* @param bound
|
||||
* @param options
|
||||
*/
|
||||
getElementsByBound(
|
||||
bound: IBound | Bound,
|
||||
options?: { type: 'all' }
|
||||
): GfxModel[];
|
||||
|
||||
getElementsByBound(
|
||||
bound: IBound | Bound,
|
||||
options: { type: 'canvas' }
|
||||
): GfxPrimitiveElementModel[];
|
||||
|
||||
getElementsByBound(
|
||||
bound: IBound | Bound,
|
||||
options: { type: 'block' }
|
||||
): GfxBlockElementModel[];
|
||||
|
||||
getElementsByBound(
|
||||
bound: IBound | Bound,
|
||||
options: { type: 'block' | 'canvas' | 'all' } = {
|
||||
type: 'all',
|
||||
}
|
||||
): GfxModel[] {
|
||||
bound = bound instanceof Bound ? bound : Bound.from(bound);
|
||||
|
||||
let candidates = this.grid.search(bound);
|
||||
|
||||
if (options.type !== 'all') {
|
||||
const filter =
|
||||
options.type === 'block'
|
||||
? (elm: GfxModel) => elm instanceof GfxBlockElementModel
|
||||
: (elm: GfxModel) => elm instanceof GfxPrimitiveElementModel;
|
||||
|
||||
candidates = candidates.filter(filter);
|
||||
}
|
||||
|
||||
candidates.sort(this.layer.compare);
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
getElementsByType(type: string): (GfxModel | BlockModel<object>)[] {
|
||||
return (
|
||||
this.surface?.getElementsByType(type) ??
|
||||
this.doc.getBlocksByFlavour(type).map(b => b.model)
|
||||
);
|
||||
}
|
||||
|
||||
override mounted() {
|
||||
this.viewport.setViewportElement(this.std.host);
|
||||
this.std.provider.getAll(GfxExtensionIdentifier).forEach(ext => {
|
||||
ext.mounted();
|
||||
});
|
||||
}
|
||||
|
||||
override unmounted() {
|
||||
this.std.provider.getAll(GfxExtensionIdentifier).forEach(ext => {
|
||||
ext.unmounted();
|
||||
});
|
||||
this.viewport.clearViewportElement();
|
||||
this._disposables.dispose();
|
||||
}
|
||||
|
||||
updateElement(
|
||||
element: GfxModel | string,
|
||||
props: Record<string, unknown>
|
||||
): void {
|
||||
const elemId = typeof element === 'string' ? element : element.id;
|
||||
|
||||
if (this.surface?.hasElementById(elemId)) {
|
||||
this.surface.updateElement(elemId, props);
|
||||
} else {
|
||||
const block = this.doc.getBlock(elemId);
|
||||
block && this.doc.updateBlock(block.model, props);
|
||||
}
|
||||
}
|
||||
}
|
||||
54
blocksuite/framework/block-std/src/gfx/cursor.ts
Normal file
54
blocksuite/framework/block-std/src/gfx/cursor.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
export type StandardCursor =
|
||||
| 'default'
|
||||
| 'pointer'
|
||||
| 'move'
|
||||
| 'text'
|
||||
| 'crosshair'
|
||||
| 'not-allowed'
|
||||
| 'grab'
|
||||
| 'grabbing'
|
||||
| 'nwse-resize'
|
||||
| 'nesw-resize'
|
||||
| 'ew-resize'
|
||||
| 'ns-resize'
|
||||
| 'n-resize'
|
||||
| 's-resize'
|
||||
| 'w-resize'
|
||||
| 'e-resize'
|
||||
| 'ne-resize'
|
||||
| 'se-resize'
|
||||
| 'sw-resize'
|
||||
| 'nw-resize'
|
||||
| 'zoom-in'
|
||||
| 'zoom-out'
|
||||
| 'help'
|
||||
| 'wait'
|
||||
| 'progress'
|
||||
| 'copy'
|
||||
| 'alias'
|
||||
| 'context-menu'
|
||||
| 'cell'
|
||||
| 'vertical-text'
|
||||
| 'no-drop'
|
||||
| 'not-allowed'
|
||||
| 'all-scroll'
|
||||
| 'col-resize'
|
||||
| 'row-resize'
|
||||
| 'none'
|
||||
| 'inherit'
|
||||
| 'initial'
|
||||
| 'unset';
|
||||
|
||||
export type URLCursor = `url(${string})`;
|
||||
|
||||
export type URLCursorWithCoords = `url(${string}) ${number} ${number}`;
|
||||
|
||||
export type URLCursorWithFallback =
|
||||
| `${URLCursor}, ${StandardCursor}`
|
||||
| `${URLCursorWithCoords}, ${StandardCursor}`;
|
||||
|
||||
export type CursorType =
|
||||
| StandardCursor
|
||||
| URLCursor
|
||||
| URLCursorWithCoords
|
||||
| URLCursorWithFallback;
|
||||
53
blocksuite/framework/block-std/src/gfx/extension.ts
Normal file
53
blocksuite/framework/block-std/src/gfx/extension.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { type Container, createIdentifier } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
import { Extension } from '../extension/extension.js';
|
||||
import type { GfxController } from './controller.js';
|
||||
import { GfxControllerIdentifier } from './identifiers.js';
|
||||
|
||||
export const GfxExtensionIdentifier =
|
||||
createIdentifier<GfxExtension>('GfxExtension');
|
||||
|
||||
export const GfxClassExtenderIdentifier = createIdentifier<{
|
||||
extendFn: (gfx: GfxController) => void;
|
||||
}>('GfxClassExtender');
|
||||
|
||||
export abstract class GfxExtension extends Extension {
|
||||
static key: string;
|
||||
|
||||
get std() {
|
||||
return this.gfx.std;
|
||||
}
|
||||
|
||||
constructor(protected readonly gfx: GfxController) {
|
||||
super();
|
||||
}
|
||||
|
||||
// This method is used to extend the GfxController
|
||||
static extendGfx(_: GfxController) {}
|
||||
|
||||
static override setup(di: Container) {
|
||||
if (!this.key) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'key is not defined in the GfxExtension'
|
||||
);
|
||||
}
|
||||
|
||||
di.addImpl(GfxClassExtenderIdentifier(this.key), {
|
||||
extendFn: this.extendGfx,
|
||||
});
|
||||
|
||||
di.add(this as unknown as { new (gfx: GfxController): GfxExtension }, [
|
||||
GfxControllerIdentifier,
|
||||
]);
|
||||
|
||||
di.addImpl(GfxExtensionIdentifier(this.key), provider =>
|
||||
provider.get(this)
|
||||
);
|
||||
}
|
||||
|
||||
mounted() {}
|
||||
|
||||
unmounted() {}
|
||||
}
|
||||
471
blocksuite/framework/block-std/src/gfx/grid.ts
Normal file
471
blocksuite/framework/block-std/src/gfx/grid.ts
Normal file
@@ -0,0 +1,471 @@
|
||||
import type { IBound } from '@blocksuite/global/utils';
|
||||
import {
|
||||
Bound,
|
||||
getBoundWithRotation,
|
||||
intersects,
|
||||
} from '@blocksuite/global/utils';
|
||||
import type { BlockModel, Doc } from '@blocksuite/store';
|
||||
|
||||
import { compare } from '../utils/layer.js';
|
||||
import { GfxBlockElementModel } from './model/gfx-block-model.js';
|
||||
import type { GfxModel } from './model/model.js';
|
||||
import { GfxPrimitiveElementModel } from './model/surface/element-model.js';
|
||||
import { GfxLocalElementModel } from './model/surface/local-element-model.js';
|
||||
import { SurfaceBlockModel } from './model/surface/surface-model.js';
|
||||
|
||||
function getGridIndex(val: number) {
|
||||
return Math.ceil(val / DEFAULT_GRID_SIZE) - 1;
|
||||
}
|
||||
|
||||
function rangeFromBound(a: IBound): number[] {
|
||||
if (a.rotate) a = getBoundWithRotation(a);
|
||||
const minRow = getGridIndex(a.x);
|
||||
const maxRow = getGridIndex(a.x + a.w);
|
||||
const minCol = getGridIndex(a.y);
|
||||
const maxCol = getGridIndex(a.y + a.h);
|
||||
return [minRow, maxRow, minCol, maxCol];
|
||||
}
|
||||
|
||||
function rangeFromElement(ele: GfxModel | GfxLocalElementModel): number[] {
|
||||
const bound = ele.elementBound;
|
||||
|
||||
bound.w += ele.responseExtension[0] * 2;
|
||||
bound.h += ele.responseExtension[1] * 2;
|
||||
bound.x -= ele.responseExtension[0];
|
||||
bound.y -= ele.responseExtension[1];
|
||||
|
||||
const minRow = getGridIndex(bound.x);
|
||||
const maxRow = getGridIndex(bound.maxX);
|
||||
const minCol = getGridIndex(bound.y);
|
||||
const maxCol = getGridIndex(bound.maxY);
|
||||
return [minRow, maxRow, minCol, maxCol];
|
||||
}
|
||||
|
||||
function rangeFromElementExternal(ele: GfxModel): number[] | null {
|
||||
if (!ele.externalXYWH) return null;
|
||||
|
||||
const bound = Bound.deserialize(ele.externalXYWH);
|
||||
const minRow = getGridIndex(bound.x);
|
||||
const maxRow = getGridIndex(bound.maxX);
|
||||
const minCol = getGridIndex(bound.y);
|
||||
const maxCol = getGridIndex(bound.maxY);
|
||||
return [minRow, maxRow, minCol, maxCol];
|
||||
}
|
||||
|
||||
export const DEFAULT_GRID_SIZE = 3000;
|
||||
|
||||
const typeFilters = {
|
||||
block: (model: GfxModel | GfxLocalElementModel) =>
|
||||
model instanceof GfxBlockElementModel,
|
||||
canvas: (model: GfxModel | GfxLocalElementModel) =>
|
||||
model instanceof GfxPrimitiveElementModel,
|
||||
local: (model: GfxModel | GfxLocalElementModel) =>
|
||||
model instanceof GfxLocalElementModel,
|
||||
};
|
||||
|
||||
type FilterFunc = (model: GfxModel | GfxLocalElementModel) => boolean;
|
||||
|
||||
export class GridManager {
|
||||
private _elementToGrids = new Map<
|
||||
GfxModel | GfxLocalElementModel,
|
||||
Set<Set<GfxModel | GfxLocalElementModel>>
|
||||
>();
|
||||
|
||||
private _externalElementToGrids = new Map<GfxModel, Set<Set<GfxModel>>>();
|
||||
|
||||
private _externalGrids = new Map<string, Set<GfxModel>>();
|
||||
|
||||
private _grids = new Map<string, Set<GfxModel | GfxLocalElementModel>>();
|
||||
|
||||
get isEmpty() {
|
||||
return this._grids.size === 0;
|
||||
}
|
||||
|
||||
private _addToExternalGrids(element: GfxModel) {
|
||||
const range = rangeFromElementExternal(element);
|
||||
|
||||
if (!range) {
|
||||
this._removeFromExternalGrids(element);
|
||||
return;
|
||||
}
|
||||
|
||||
const [minRow, maxRow, minCol, maxCol] = range;
|
||||
const grids = new Set<Set<GfxModel>>();
|
||||
this._externalElementToGrids.set(element, grids);
|
||||
|
||||
for (let i = minRow; i <= maxRow; i++) {
|
||||
for (let j = minCol; j <= maxCol; j++) {
|
||||
let grid = this._getExternalGrid(i, j);
|
||||
if (!grid) {
|
||||
grid = this._createExternalGrid(i, j);
|
||||
}
|
||||
grid.add(element);
|
||||
grids.add(grid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _createExternalGrid(row: number, col: number) {
|
||||
const id = row + '|' + col;
|
||||
const elements = new Set<GfxModel>();
|
||||
this._externalGrids.set(id, elements);
|
||||
return elements;
|
||||
}
|
||||
|
||||
private _createGrid(row: number, col: number) {
|
||||
const id = row + '|' + col;
|
||||
const elements = new Set<GfxModel>();
|
||||
this._grids.set(id, elements);
|
||||
return elements;
|
||||
}
|
||||
|
||||
private _getExternalGrid(row: number, col: number) {
|
||||
const id = row + '|' + col;
|
||||
return this._externalGrids.get(id);
|
||||
}
|
||||
|
||||
private _getGrid(row: number, col: number) {
|
||||
const id = row + '|' + col;
|
||||
return this._grids.get(id);
|
||||
}
|
||||
|
||||
private _removeFromExternalGrids(element: GfxModel) {
|
||||
const grids = this._externalElementToGrids.get(element);
|
||||
if (grids) {
|
||||
for (const grid of grids) {
|
||||
grid.delete(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _searchExternal(
|
||||
bound: IBound,
|
||||
options: { filterFunc: FilterFunc; strict: boolean }
|
||||
): Set<GfxModel> {
|
||||
const [minRow, maxRow, minCol, maxCol] = rangeFromBound(bound);
|
||||
const results = new Set<GfxModel>();
|
||||
const b = Bound.from(bound);
|
||||
|
||||
for (let i = minRow; i <= maxRow; i++) {
|
||||
for (let j = minCol; j <= maxCol; j++) {
|
||||
const gridElements = this._getExternalGrid(i, j);
|
||||
if (!gridElements) continue;
|
||||
|
||||
for (const element of gridElements) {
|
||||
const externalBound = element.externalBound;
|
||||
if (
|
||||
options.filterFunc(element) &&
|
||||
externalBound &&
|
||||
(options.strict
|
||||
? b.contains(externalBound)
|
||||
: intersects(externalBound, bound))
|
||||
) {
|
||||
results.add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results;
|
||||
}
|
||||
|
||||
private _toFilterFunc(filters: (keyof typeof typeFilters | FilterFunc)[]) {
|
||||
const filterFuncs: FilterFunc[] = filters.map(filter => {
|
||||
if (typeof filter === 'function') {
|
||||
return filter;
|
||||
}
|
||||
return typeFilters[filter];
|
||||
});
|
||||
|
||||
return (model: GfxModel | GfxLocalElementModel) =>
|
||||
filterFuncs.some(filter => filter(model));
|
||||
}
|
||||
|
||||
add(element: GfxModel | GfxLocalElementModel) {
|
||||
if (!(element instanceof GfxLocalElementModel)) {
|
||||
this._addToExternalGrids(element);
|
||||
}
|
||||
|
||||
const [minRow, maxRow, minCol, maxCol] = rangeFromElement(element);
|
||||
const grids = new Set<Set<GfxModel | GfxLocalElementModel>>();
|
||||
this._elementToGrids.set(element, grids);
|
||||
|
||||
for (let i = minRow; i <= maxRow; i++) {
|
||||
for (let j = minCol; j <= maxCol; j++) {
|
||||
let grid = this._getGrid(i, j);
|
||||
if (!grid) {
|
||||
grid = this._createGrid(i, j);
|
||||
}
|
||||
grid.add(element);
|
||||
grids.add(grid);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
boundHasChanged(a: IBound, b: IBound) {
|
||||
const [minRow, maxRow, minCol, maxCol] = rangeFromBound(a);
|
||||
const [minRow2, maxRow2, minCol2, maxCol2] = rangeFromBound(b);
|
||||
return (
|
||||
minRow !== minRow2 ||
|
||||
maxRow !== maxRow2 ||
|
||||
minCol !== minCol2 ||
|
||||
maxCol !== maxCol2
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param bound
|
||||
* @param strict
|
||||
* @param reverseChecking If true, check if the bound is inside the elements instead of checking if the elements are inside the bound
|
||||
* @returns
|
||||
*/
|
||||
has(
|
||||
bound: IBound,
|
||||
strict: boolean = false,
|
||||
reverseChecking: boolean = false,
|
||||
filter?: (model: GfxModel | GfxLocalElementModel) => boolean
|
||||
) {
|
||||
const [minRow, maxRow, minCol, maxCol] = rangeFromBound(bound);
|
||||
const b = Bound.from(bound);
|
||||
const check = reverseChecking
|
||||
? (target: Bound) => {
|
||||
return strict ? target.contains(b) : intersects(b, target);
|
||||
}
|
||||
: (target: Bound) => {
|
||||
return strict ? b.contains(target) : intersects(target, b);
|
||||
};
|
||||
|
||||
for (let i = minRow; i <= maxRow; i++) {
|
||||
for (let j = minCol; j <= maxCol; j++) {
|
||||
const gridElements = this._getGrid(i, j);
|
||||
if (!gridElements) continue;
|
||||
for (const element of gridElements) {
|
||||
if ((!filter || filter(element)) && check(element.elementBound)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
remove(element: GfxModel | GfxLocalElementModel) {
|
||||
const grids = this._elementToGrids.get(element);
|
||||
if (grids) {
|
||||
for (const grid of grids) {
|
||||
grid.delete(element);
|
||||
}
|
||||
}
|
||||
this._elementToGrids.delete(element);
|
||||
|
||||
if (!(element instanceof GfxLocalElementModel)) {
|
||||
this._removeFromExternalGrids(element);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for elements in a bound.
|
||||
* @param bound
|
||||
* @param options
|
||||
*/
|
||||
search<T extends keyof typeof typeFilters>(
|
||||
bound: IBound,
|
||||
options?: {
|
||||
/**
|
||||
* If true, only return elements that are completely inside the bound.
|
||||
* Default is false.
|
||||
*/
|
||||
strict?: boolean;
|
||||
/**
|
||||
* If true, return a set of elements instead of an array
|
||||
*/
|
||||
useSet?: false;
|
||||
/**
|
||||
* Use this to filter the elements, if not provided, it will return blocks and canvas elements by default
|
||||
*/
|
||||
filter?: (T | FilterFunc)[] | FilterFunc;
|
||||
}
|
||||
): T extends 'local'[] ? (GfxModel | GfxLocalElementModel)[] : GfxModel[];
|
||||
search<T extends keyof typeof typeFilters>(
|
||||
bound: IBound,
|
||||
options: {
|
||||
strict?: boolean | undefined;
|
||||
useSet: true;
|
||||
filter?: (T | FilterFunc)[] | FilterFunc;
|
||||
}
|
||||
): T extends 'local'[] ? Set<GfxModel | GfxLocalElementModel> : Set<GfxModel>;
|
||||
search<T extends keyof typeof typeFilters>(
|
||||
bound: IBound,
|
||||
options: {
|
||||
strict?: boolean;
|
||||
useSet?: boolean;
|
||||
filter?: (T | FilterFunc)[] | FilterFunc;
|
||||
} = {
|
||||
useSet: false,
|
||||
}
|
||||
):
|
||||
| (GfxModel | GfxLocalElementModel)[]
|
||||
| Set<GfxModel | GfxLocalElementModel> {
|
||||
const strict = options.strict ?? false;
|
||||
const [minRow, maxRow, minCol, maxCol] = rangeFromBound(bound);
|
||||
const b = Bound.from(bound);
|
||||
const returnSet = options.useSet ?? false;
|
||||
const filterFunc =
|
||||
(Array.isArray(options.filter)
|
||||
? this._toFilterFunc(options.filter)
|
||||
: options.filter) ?? this._toFilterFunc(['canvas', 'block']);
|
||||
const results: Set<GfxModel | GfxLocalElementModel> = this._searchExternal(
|
||||
bound,
|
||||
{
|
||||
filterFunc,
|
||||
strict,
|
||||
}
|
||||
);
|
||||
|
||||
for (let i = minRow; i <= maxRow; i++) {
|
||||
for (let j = minCol; j <= maxCol; j++) {
|
||||
const gridElements = this._getGrid(i, j);
|
||||
if (!gridElements) continue;
|
||||
for (const element of gridElements) {
|
||||
if (
|
||||
!(element as GfxPrimitiveElementModel).hidden &&
|
||||
filterFunc(element) &&
|
||||
(strict
|
||||
? b.contains(element.elementBound)
|
||||
: intersects(element.responseBound, b))
|
||||
) {
|
||||
results.add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (returnSet) return results;
|
||||
|
||||
// sort elements in set based on index
|
||||
const sorted = Array.from(results).sort(compare);
|
||||
|
||||
return sorted;
|
||||
}
|
||||
|
||||
update(element: GfxModel | GfxLocalElementModel) {
|
||||
this.remove(element);
|
||||
this.add(element);
|
||||
}
|
||||
|
||||
watch(blocks: { doc?: Doc; surface?: SurfaceBlockModel | null }) {
|
||||
const disposables: { dispose: () => void }[] = [];
|
||||
const { doc, surface } = blocks;
|
||||
const isRenderableBlock = (
|
||||
block: BlockModel
|
||||
): block is GfxBlockElementModel => {
|
||||
return (
|
||||
block instanceof GfxBlockElementModel &&
|
||||
(block.parent?.role === 'root' ||
|
||||
block.parent instanceof SurfaceBlockModel)
|
||||
);
|
||||
};
|
||||
|
||||
if (doc) {
|
||||
disposables.push(
|
||||
doc.slots.blockUpdated.on(payload => {
|
||||
if (payload.type === 'add' && isRenderableBlock(payload.model)) {
|
||||
this.add(payload.model);
|
||||
}
|
||||
|
||||
if (payload.type === 'update') {
|
||||
const model = doc.getBlock(payload.id)
|
||||
?.model as GfxBlockElementModel;
|
||||
|
||||
if (!model) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this._elementToGrids.has(model) && !isRenderableBlock(model)) {
|
||||
this.remove(model as GfxBlockElementModel);
|
||||
} else if (
|
||||
payload.props.key === 'xywh' &&
|
||||
isRenderableBlock(model)
|
||||
) {
|
||||
this.update(
|
||||
doc.getBlock(payload.id)?.model as GfxBlockElementModel
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
payload.type === 'delete' &&
|
||||
payload.model instanceof GfxBlockElementModel
|
||||
) {
|
||||
this.remove(payload.model);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
Object.values(doc.blocks.peek()).forEach(block => {
|
||||
if (isRenderableBlock(block.model)) {
|
||||
this.add(block.model);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if (surface) {
|
||||
disposables.push(
|
||||
surface.elementAdded.on(payload => {
|
||||
this.add(surface.getElementById(payload.id)!);
|
||||
})
|
||||
);
|
||||
|
||||
disposables.push(
|
||||
surface.elementRemoved.on(payload => {
|
||||
this.remove(payload.model);
|
||||
})
|
||||
);
|
||||
|
||||
disposables.push(
|
||||
surface.elementUpdated.on(payload => {
|
||||
if (
|
||||
payload.props['xywh'] ||
|
||||
payload.props['externalXYWH'] ||
|
||||
payload.props['responseExtension']
|
||||
) {
|
||||
this.update(surface.getElementById(payload.id)!);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
disposables.push(
|
||||
surface.localElementAdded.on(elm => {
|
||||
this.add(elm);
|
||||
})
|
||||
);
|
||||
|
||||
disposables.push(
|
||||
surface.localElementUpdated.on(payload => {
|
||||
if (payload.props['xywh'] || payload.props['responseExtension']) {
|
||||
this.update(payload.model);
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
disposables.push(
|
||||
surface.localElementDeleted.on(elm => {
|
||||
this.remove(elm);
|
||||
})
|
||||
);
|
||||
|
||||
surface.elementModels.forEach(model => {
|
||||
this.add(model);
|
||||
});
|
||||
surface.localElementModels.forEach(model => {
|
||||
this.add(model);
|
||||
});
|
||||
}
|
||||
|
||||
return () => {
|
||||
disposables.forEach(d => d.dispose());
|
||||
};
|
||||
}
|
||||
}
|
||||
10
blocksuite/framework/block-std/src/gfx/identifiers.ts
Normal file
10
blocksuite/framework/block-std/src/gfx/identifiers.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { ServiceIdentifier } from '@blocksuite/global/di';
|
||||
|
||||
import { LifeCycleWatcherIdentifier } from '../identifier.js';
|
||||
import type { GfxController } from './controller.js';
|
||||
|
||||
export const gfxControllerKey = 'GfxController';
|
||||
|
||||
export const GfxControllerIdentifier = LifeCycleWatcherIdentifier(
|
||||
gfxControllerKey
|
||||
) as ServiceIdentifier<GfxController>;
|
||||
86
blocksuite/framework/block-std/src/gfx/index.ts
Normal file
86
blocksuite/framework/block-std/src/gfx/index.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
export { generateKeyBetweenV2 } from '../utils/fractional-indexing.js';
|
||||
export {
|
||||
compare as compareLayer,
|
||||
renderableInEdgeless,
|
||||
SortOrder,
|
||||
} from '../utils/layer.js';
|
||||
export {
|
||||
canSafeAddToContainer,
|
||||
descendantElementsImpl,
|
||||
getTopElements,
|
||||
hasDescendantElementImpl,
|
||||
} from '../utils/tree.js';
|
||||
export { GfxController } from './controller.js';
|
||||
export type { CursorType, StandardCursor } from './cursor.js';
|
||||
export { GfxExtension, GfxExtensionIdentifier } from './extension.js';
|
||||
export { GridManager } from './grid.js';
|
||||
export { GfxControllerIdentifier } from './identifiers.js';
|
||||
export { LayerManager, type ReorderingDirection } from './layer.js';
|
||||
export type {
|
||||
GfxCompatibleInterface,
|
||||
GfxElementGeometry,
|
||||
GfxGroupCompatibleInterface,
|
||||
PointTestOptions,
|
||||
} from './model/base.js';
|
||||
export {
|
||||
gfxGroupCompatibleSymbol,
|
||||
isGfxGroupCompatibleModel,
|
||||
} from './model/base.js';
|
||||
export {
|
||||
GfxBlockElementModel,
|
||||
type GfxCommonBlockProps,
|
||||
GfxCompatibleBlockModel as GfxCompatible,
|
||||
type GfxCompatibleProps,
|
||||
} from './model/gfx-block-model.js';
|
||||
export { type GfxModel } from './model/model.js';
|
||||
export {
|
||||
convert,
|
||||
convertProps,
|
||||
derive,
|
||||
field,
|
||||
getDerivedProps,
|
||||
getFieldPropsSet,
|
||||
initializeObservers,
|
||||
initializeWatchers,
|
||||
local,
|
||||
observe,
|
||||
updateDerivedProps,
|
||||
watch,
|
||||
} from './model/surface/decorators/index.js';
|
||||
export {
|
||||
type BaseElementProps,
|
||||
GfxGroupLikeElementModel,
|
||||
GfxPrimitiveElementModel,
|
||||
type SerializedElement,
|
||||
} from './model/surface/element-model.js';
|
||||
export {
|
||||
GfxLocalElementModel,
|
||||
prop,
|
||||
} from './model/surface/local-element-model.js';
|
||||
export {
|
||||
SurfaceBlockModel,
|
||||
type SurfaceBlockProps,
|
||||
type SurfaceMiddleware,
|
||||
} from './model/surface/surface-model.js';
|
||||
export { GfxSelectionManager } from './selection.js';
|
||||
export {
|
||||
SurfaceMiddlewareBuilder,
|
||||
SurfaceMiddlewareExtension,
|
||||
} from './surface-middleware.js';
|
||||
export {
|
||||
BaseTool,
|
||||
type GfxToolsFullOption,
|
||||
type GfxToolsFullOptionValue,
|
||||
type GfxToolsMap,
|
||||
type GfxToolsOption,
|
||||
} from './tool/tool.js';
|
||||
export { MouseButton, ToolController } from './tool/tool-controller.js';
|
||||
export {
|
||||
type EventsHandlerMap,
|
||||
GfxElementModelView,
|
||||
type SupportedEvent,
|
||||
} from './view/view.js';
|
||||
export { ViewManager } from './view/view-manager.js';
|
||||
export * from './viewport.js';
|
||||
export { GfxViewportElement } from './viewport-element.js';
|
||||
export { generateKeyBetween, generateNKeysBetween } from 'fractional-indexing';
|
||||
51
blocksuite/framework/block-std/src/gfx/keyboard.ts
Normal file
51
blocksuite/framework/block-std/src/gfx/keyboard.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import { DisposableGroup } from '@blocksuite/global/utils';
|
||||
import { Signal } from '@preact/signals-core';
|
||||
|
||||
import type { BlockStdScope } from '../scope/block-std-scope.js';
|
||||
|
||||
export class KeyboardController {
|
||||
private _disposable = new DisposableGroup();
|
||||
|
||||
shiftKey$ = new Signal<boolean>(false);
|
||||
|
||||
spaceKey$ = new Signal<boolean>(false);
|
||||
|
||||
constructor(readonly std: BlockStdScope) {
|
||||
this._init();
|
||||
}
|
||||
|
||||
private _init() {
|
||||
this._disposable.add(
|
||||
this._listenKeyboard('keydown', evt => {
|
||||
this.shiftKey$.value = evt.shiftKey && evt.key === 'Shift';
|
||||
this.spaceKey$.value = evt.code === 'Space';
|
||||
})
|
||||
);
|
||||
|
||||
this._disposable.add(
|
||||
this._listenKeyboard('keyup', evt => {
|
||||
this.shiftKey$.value =
|
||||
evt.shiftKey && evt.key === 'Shift' ? true : false;
|
||||
|
||||
if (evt.code === 'Space') {
|
||||
this.spaceKey$.value = false;
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
private _listenKeyboard(
|
||||
event: 'keydown' | 'keyup',
|
||||
callback: (keyboardEvt: KeyboardEvent) => void
|
||||
) {
|
||||
document.addEventListener(event, callback, false);
|
||||
|
||||
return () => {
|
||||
document.removeEventListener(event, callback, false);
|
||||
};
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this._disposable.dispose();
|
||||
}
|
||||
}
|
||||
871
blocksuite/framework/block-std/src/gfx/layer.ts
Normal file
871
blocksuite/framework/block-std/src/gfx/layer.ts
Normal file
@@ -0,0 +1,871 @@
|
||||
import {
|
||||
assertType,
|
||||
Bound,
|
||||
DisposableGroup,
|
||||
last,
|
||||
Slot,
|
||||
} from '@blocksuite/global/utils';
|
||||
import type { Doc } from '@blocksuite/store';
|
||||
import { generateKeyBetween } from 'fractional-indexing';
|
||||
|
||||
import {
|
||||
compare,
|
||||
getElementIndex,
|
||||
getLayerEndZIndex,
|
||||
insertToOrderedArray,
|
||||
isInRange,
|
||||
removeFromOrderedArray,
|
||||
SortOrder,
|
||||
ungroupIndex,
|
||||
updateLayersZIndex,
|
||||
} from '../utils/layer.js';
|
||||
import {
|
||||
type GfxGroupCompatibleInterface,
|
||||
isGfxGroupCompatibleModel,
|
||||
} from './model/base.js';
|
||||
import { GfxBlockElementModel } from './model/gfx-block-model.js';
|
||||
import type { GfxModel } from './model/model.js';
|
||||
import { GfxPrimitiveElementModel } from './model/surface/element-model.js';
|
||||
import { GfxLocalElementModel } from './model/surface/local-element-model.js';
|
||||
import { SurfaceBlockModel } from './model/surface/surface-model.js';
|
||||
|
||||
export type ReorderingDirection = 'front' | 'forward' | 'backward' | 'back';
|
||||
|
||||
type BaseLayer<T> = {
|
||||
set: Set<T>;
|
||||
|
||||
elements: Array<T>;
|
||||
|
||||
/**
|
||||
* fractional indexing range
|
||||
*/
|
||||
indexes: [string, string];
|
||||
};
|
||||
|
||||
export type BlockLayer = BaseLayer<GfxBlockElementModel> & {
|
||||
type: 'block';
|
||||
|
||||
/**
|
||||
* The z-index of the first block in this layer.
|
||||
*
|
||||
* A block layer may contains multiple blocks,
|
||||
* the block should be rendered with this `zIndex` + "its index in the layer" as the z-index property.
|
||||
*/
|
||||
zIndex: number;
|
||||
};
|
||||
|
||||
export type CanvasLayer = BaseLayer<GfxPrimitiveElementModel> & {
|
||||
type: 'canvas';
|
||||
|
||||
/**
|
||||
* The z-index of canvas layer.
|
||||
*
|
||||
* A canvas layer renders all the elements in a single canvas,
|
||||
* this property is used to render the canvas with correct z-index.
|
||||
*/
|
||||
zIndex: number;
|
||||
};
|
||||
|
||||
export type Layer = BlockLayer | CanvasLayer;
|
||||
|
||||
export class LayerManager {
|
||||
static INITIAL_INDEX = 'a0';
|
||||
|
||||
private _disposable = new DisposableGroup();
|
||||
|
||||
blocks: GfxBlockElementModel[] = [];
|
||||
|
||||
canvasElements: GfxPrimitiveElementModel[] = [];
|
||||
|
||||
canvasLayers: {
|
||||
set: Set<GfxPrimitiveElementModel>;
|
||||
/**
|
||||
* fractional index
|
||||
*/
|
||||
indexes: [string, string];
|
||||
/**
|
||||
* z-index, used for actual rendering
|
||||
*/
|
||||
zIndex: number;
|
||||
elements: Array<GfxPrimitiveElementModel>;
|
||||
}[] = [];
|
||||
|
||||
layers: Layer[] = [];
|
||||
|
||||
slots = {
|
||||
layerUpdated: new Slot<{
|
||||
type: 'delete' | 'add' | 'update';
|
||||
initiatingElement: GfxModel | GfxLocalElementModel;
|
||||
}>(),
|
||||
};
|
||||
|
||||
constructor(
|
||||
private _doc: Doc,
|
||||
private _surface: SurfaceBlockModel | null,
|
||||
options: {
|
||||
watch: boolean;
|
||||
} = { watch: true }
|
||||
) {
|
||||
this._reset();
|
||||
|
||||
if (options?.watch) {
|
||||
this.watch({
|
||||
doc: _doc,
|
||||
surface: _surface,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private _buildCanvasLayers() {
|
||||
const canvasLayers = this.layers
|
||||
.filter<CanvasLayer>(
|
||||
(layer): layer is CanvasLayer => layer.type === 'canvas'
|
||||
)
|
||||
.map(layer => {
|
||||
return {
|
||||
set: layer.set,
|
||||
elements: layer.elements,
|
||||
zIndex: layer.zIndex,
|
||||
indexes: layer.indexes,
|
||||
};
|
||||
}) as LayerManager['canvasLayers'];
|
||||
|
||||
if (!canvasLayers.length || last(this.layers)?.type !== 'canvas') {
|
||||
canvasLayers.push({
|
||||
set: new Set(),
|
||||
elements: [],
|
||||
zIndex: 0,
|
||||
indexes: [LayerManager.INITIAL_INDEX, LayerManager.INITIAL_INDEX],
|
||||
});
|
||||
}
|
||||
|
||||
this.canvasLayers = canvasLayers;
|
||||
}
|
||||
|
||||
private _getModelType(
|
||||
element: GfxModel | GfxLocalElementModel
|
||||
): 'block' | 'canvas' {
|
||||
return element instanceof GfxLocalElementModel ||
|
||||
element instanceof GfxPrimitiveElementModel
|
||||
? 'canvas'
|
||||
: 'block';
|
||||
}
|
||||
|
||||
private _initLayers() {
|
||||
let blockIdx = 0;
|
||||
let canvasIdx = 0;
|
||||
const layers: LayerManager['layers'] = [];
|
||||
let curLayer: LayerManager['layers'][number] | undefined;
|
||||
let currentCSSZindex = 1;
|
||||
|
||||
const pushCurLayer = () => {
|
||||
if (curLayer) {
|
||||
curLayer.indexes = [
|
||||
getElementIndex(curLayer.elements[0]),
|
||||
getElementIndex(last(curLayer.elements)!),
|
||||
];
|
||||
curLayer.zIndex = currentCSSZindex;
|
||||
layers.push(curLayer as LayerManager['layers'][number]);
|
||||
|
||||
currentCSSZindex +=
|
||||
curLayer.type === 'block' ? curLayer.elements.length : 1;
|
||||
}
|
||||
};
|
||||
const addLayer = (type: 'canvas' | 'block') => {
|
||||
pushCurLayer();
|
||||
curLayer =
|
||||
type === 'canvas'
|
||||
? ({
|
||||
type,
|
||||
indexes: [LayerManager.INITIAL_INDEX, LayerManager.INITIAL_INDEX],
|
||||
zIndex: 0,
|
||||
set: new Set(),
|
||||
elements: [],
|
||||
bound: new Bound(),
|
||||
} as CanvasLayer)
|
||||
: ({
|
||||
type,
|
||||
indexes: [LayerManager.INITIAL_INDEX, LayerManager.INITIAL_INDEX],
|
||||
zIndex: 0,
|
||||
set: new Set(),
|
||||
elements: [],
|
||||
} as BlockLayer);
|
||||
};
|
||||
|
||||
while (
|
||||
blockIdx < this.blocks.length ||
|
||||
canvasIdx < this.canvasElements.length
|
||||
) {
|
||||
const curBlock = this.blocks[blockIdx];
|
||||
const curCanvas = this.canvasElements[canvasIdx];
|
||||
|
||||
if (!curBlock && !curCanvas) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (!curBlock) {
|
||||
if (curLayer?.type !== 'canvas') {
|
||||
addLayer('canvas');
|
||||
}
|
||||
assertType<CanvasLayer>(curLayer);
|
||||
|
||||
const remains = this.canvasElements.slice(canvasIdx);
|
||||
|
||||
curLayer!.elements = curLayer.elements.concat(remains);
|
||||
remains.forEach(element => (curLayer as CanvasLayer).set.add(element));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (!curCanvas) {
|
||||
if (curLayer?.type !== 'block') {
|
||||
addLayer('block');
|
||||
}
|
||||
|
||||
assertType<BlockLayer>(curLayer);
|
||||
|
||||
const remains = this.blocks.slice(blockIdx);
|
||||
|
||||
curLayer.elements = curLayer.elements.concat(remains);
|
||||
remains.forEach(block => (curLayer as BlockLayer).set.add(block));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
const order = compare(curBlock, curCanvas);
|
||||
|
||||
switch (order) {
|
||||
case -1:
|
||||
if (curLayer?.type !== 'block') {
|
||||
addLayer('block');
|
||||
}
|
||||
|
||||
assertType<BlockLayer>(curLayer);
|
||||
|
||||
curLayer!.set.add(curBlock);
|
||||
curLayer!.elements.push(curBlock);
|
||||
|
||||
++blockIdx;
|
||||
|
||||
break;
|
||||
case 1:
|
||||
if (curLayer?.type !== 'canvas') {
|
||||
addLayer('canvas');
|
||||
}
|
||||
|
||||
assertType<CanvasLayer>(curLayer);
|
||||
|
||||
curLayer!.set.add(curCanvas);
|
||||
curLayer!.elements.push(curCanvas);
|
||||
|
||||
++canvasIdx;
|
||||
|
||||
break;
|
||||
case 0:
|
||||
if (!curLayer) {
|
||||
addLayer('block');
|
||||
}
|
||||
|
||||
if (curLayer!.type === 'block') {
|
||||
curLayer!.set.add(curBlock);
|
||||
curLayer!.elements.push(curBlock);
|
||||
|
||||
++blockIdx;
|
||||
} else {
|
||||
curLayer!.set.add(curCanvas);
|
||||
curLayer!.elements.push(curCanvas);
|
||||
|
||||
++canvasIdx;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (curLayer && curLayer.elements.length) {
|
||||
pushCurLayer();
|
||||
}
|
||||
|
||||
this.layers = layers;
|
||||
this._surface?.localElementModels.forEach(el => this.add(el));
|
||||
}
|
||||
|
||||
private _insertIntoLayer(target: GfxModel, type: 'block' | 'canvas') {
|
||||
const layers = this.layers;
|
||||
let cur = layers.length - 1;
|
||||
|
||||
const addToLayer = (
|
||||
layer: Layer,
|
||||
element: GfxModel,
|
||||
position: number | 'tail'
|
||||
) => {
|
||||
assertType<CanvasLayer>(layer);
|
||||
assertType<GfxPrimitiveElementModel>(element);
|
||||
|
||||
if (position === 'tail') {
|
||||
layer.elements.push(element);
|
||||
} else {
|
||||
layer.elements.splice(position, 0, element);
|
||||
}
|
||||
|
||||
layer.set.add(element);
|
||||
|
||||
if (
|
||||
position === 'tail' ||
|
||||
position === 0 ||
|
||||
position === layer.elements.length - 1
|
||||
) {
|
||||
layer.indexes = [
|
||||
getElementIndex(layer.elements[0]),
|
||||
getElementIndex(last(layer.elements)!),
|
||||
];
|
||||
}
|
||||
};
|
||||
const createLayer = (
|
||||
type: 'block' | 'canvas',
|
||||
targets: GfxModel[],
|
||||
curZIndex: number
|
||||
): Layer => {
|
||||
const newLayer = {
|
||||
type,
|
||||
set: new Set(targets),
|
||||
indexes: [
|
||||
getElementIndex(targets[0]),
|
||||
getElementIndex(last(targets)!),
|
||||
] as [string, string],
|
||||
zIndex: curZIndex + 1,
|
||||
elements: targets,
|
||||
} as BlockLayer;
|
||||
|
||||
return newLayer as Layer;
|
||||
};
|
||||
|
||||
if (
|
||||
!last(this.layers) ||
|
||||
[SortOrder.AFTER, SortOrder.SAME].includes(
|
||||
compare(target, last(last(this.layers)!.elements)!)
|
||||
)
|
||||
) {
|
||||
const layer = last(this.layers);
|
||||
|
||||
if (layer?.type === type) {
|
||||
addToLayer(layer, target, 'tail');
|
||||
updateLayersZIndex(layers, cur);
|
||||
} else {
|
||||
this.layers.push(
|
||||
createLayer(
|
||||
type,
|
||||
[target],
|
||||
getLayerEndZIndex(layers, layers.length - 1)
|
||||
)
|
||||
);
|
||||
}
|
||||
} else {
|
||||
while (cur > -1) {
|
||||
const layer = layers[cur];
|
||||
const layerElements = layer.elements;
|
||||
|
||||
if (isInRange([layerElements[0], last(layerElements)!], target)) {
|
||||
const insertIdx = layerElements.findIndex((_, idx) => {
|
||||
const pre = layerElements[idx - 1];
|
||||
return (
|
||||
compare(target, layerElements[idx]) < 0 &&
|
||||
(!pre || compare(target, pre) >= 0)
|
||||
);
|
||||
});
|
||||
|
||||
if (layer.type === type) {
|
||||
addToLayer(layer, target, insertIdx);
|
||||
updateLayersZIndex(layers, cur);
|
||||
} else {
|
||||
const splicedElements = layer.elements.splice(insertIdx);
|
||||
layer.set = new Set(layer.elements as GfxPrimitiveElementModel[]);
|
||||
|
||||
layers.splice(
|
||||
cur + 1,
|
||||
0,
|
||||
createLayer(layer.type, splicedElements, 1)
|
||||
);
|
||||
layers.splice(cur + 1, 0, createLayer(type, [target], 1));
|
||||
updateLayersZIndex(layers, cur);
|
||||
}
|
||||
break;
|
||||
} else {
|
||||
const nextLayer = layers[cur - 1];
|
||||
|
||||
if (!nextLayer || compare(target, last(nextLayer.elements)!) >= 0) {
|
||||
if (layer.type === type) {
|
||||
addToLayer(layer, target, 0);
|
||||
updateLayersZIndex(layers, cur);
|
||||
} else {
|
||||
if (nextLayer) {
|
||||
addToLayer(nextLayer, target, 'tail');
|
||||
updateLayersZIndex(layers, cur - 1);
|
||||
} else {
|
||||
layers.unshift(createLayer(type, [target], 1));
|
||||
updateLayersZIndex(layers, 0);
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
--cur;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private _removeFromLayer(
|
||||
target: GfxModel | GfxLocalElementModel,
|
||||
type: 'block' | 'canvas'
|
||||
) {
|
||||
const layers = this.layers;
|
||||
const index = layers.findIndex(layer => {
|
||||
if (layer.type !== type) return false;
|
||||
|
||||
assertType<CanvasLayer>(layer);
|
||||
assertType<GfxPrimitiveElementModel>(target);
|
||||
|
||||
if (layer.set.has(target)) {
|
||||
layer.set.delete(target);
|
||||
const idx = layer.elements.indexOf(target);
|
||||
if (idx !== -1) {
|
||||
layer.elements.splice(layer.elements.indexOf(target), 1);
|
||||
|
||||
if (layer.elements.length) {
|
||||
layer.indexes = [
|
||||
getElementIndex(layer.elements[0]),
|
||||
getElementIndex(last(layer.elements)!),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
|
||||
if (index === -1) return;
|
||||
|
||||
const isDeletedAtEdge = index === 0 || index === layers.length - 1;
|
||||
|
||||
if (layers[index].set.size === 0) {
|
||||
if (isDeletedAtEdge) {
|
||||
layers.splice(index, 1);
|
||||
|
||||
if (layers[index]) {
|
||||
updateLayersZIndex(layers, index);
|
||||
}
|
||||
} else {
|
||||
const lastLayer = layers[index - 1] as CanvasLayer;
|
||||
const nextLayer = layers[index + 1] as CanvasLayer;
|
||||
|
||||
lastLayer.elements = lastLayer.elements.concat(nextLayer.elements);
|
||||
lastLayer.set = new Set(lastLayer.elements);
|
||||
|
||||
layers.splice(index, 2);
|
||||
updateLayersZIndex(layers, index - 1);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
updateLayersZIndex(layers, index);
|
||||
}
|
||||
|
||||
private _reset() {
|
||||
const elements = (
|
||||
this._doc
|
||||
.getBlocks()
|
||||
.filter(
|
||||
model =>
|
||||
model instanceof GfxBlockElementModel &&
|
||||
(model.parent instanceof SurfaceBlockModel ||
|
||||
model.parent?.role === 'root')
|
||||
) as GfxModel[]
|
||||
).concat(this._surface?.elementModels ?? []);
|
||||
|
||||
this.canvasElements = [];
|
||||
this.blocks = [];
|
||||
|
||||
elements.forEach(element => {
|
||||
if (element instanceof GfxPrimitiveElementModel) {
|
||||
this.canvasElements.push(element);
|
||||
} else {
|
||||
this.blocks.push(element);
|
||||
}
|
||||
});
|
||||
|
||||
this.canvasElements.sort(compare);
|
||||
this.blocks.sort(compare);
|
||||
|
||||
this._initLayers();
|
||||
this._buildCanvasLayers();
|
||||
}
|
||||
|
||||
/**
|
||||
* @returns a boolean value to indicate whether the layers have been updated
|
||||
*/
|
||||
private _updateLayer(
|
||||
element: GfxModel | GfxLocalElementModel,
|
||||
props?: Record<string, unknown>
|
||||
) {
|
||||
const modelType = this._getModelType(element);
|
||||
const isLocalElem = element instanceof GfxLocalElementModel;
|
||||
|
||||
const indexChanged = !props || 'index' in props;
|
||||
const childIdsChanged = props && 'childIds' in props;
|
||||
const shouldUpdateGroupChildren =
|
||||
isGfxGroupCompatibleModel(element) && (indexChanged || childIdsChanged);
|
||||
const updateArray = (array: GfxModel[], element: GfxModel) => {
|
||||
if (!indexChanged) return;
|
||||
removeFromOrderedArray(array, element);
|
||||
insertToOrderedArray(array, element);
|
||||
};
|
||||
|
||||
if (shouldUpdateGroupChildren) {
|
||||
this._reset();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!isLocalElem) {
|
||||
if (modelType === 'canvas') {
|
||||
updateArray(this.canvasElements, element);
|
||||
} else {
|
||||
updateArray(this.blocks, element);
|
||||
}
|
||||
}
|
||||
|
||||
if (indexChanged || childIdsChanged) {
|
||||
this._removeFromLayer(element as GfxModel, modelType);
|
||||
this._insertIntoLayer(element as GfxModel, modelType);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
add(element: GfxModel | GfxLocalElementModel) {
|
||||
const modelType = this._getModelType(element);
|
||||
const isContainer = isGfxGroupCompatibleModel(element);
|
||||
const isLocalElem = element instanceof GfxLocalElementModel;
|
||||
|
||||
if (isContainer) {
|
||||
element.childElements.forEach(child => {
|
||||
const childModelType = this._getModelType(child);
|
||||
removeFromOrderedArray(
|
||||
childModelType === 'canvas' ? this.canvasElements : this.blocks,
|
||||
child
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
if (!isLocalElem) {
|
||||
insertToOrderedArray(
|
||||
modelType === 'canvas' ? this.canvasElements : this.blocks,
|
||||
element
|
||||
);
|
||||
}
|
||||
this._insertIntoLayer(element as GfxModel, modelType);
|
||||
|
||||
if (isContainer) {
|
||||
element.childElements.forEach(child => child && this._updateLayer(child));
|
||||
}
|
||||
this._buildCanvasLayers();
|
||||
this.slots.layerUpdated.emit({
|
||||
type: 'add',
|
||||
initiatingElement: element,
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass to the `Array.sort` to sort the elements by their index
|
||||
*/
|
||||
compare(a: GfxModel, b: GfxModel) {
|
||||
return compare(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* In some cases, we need to generate a bunch of indexes in advance before acutally adding the elements to the layer manager.
|
||||
* Eg. when importing a template. The `generateIndex` is a function only depends on the current state of the manager.
|
||||
* So we cannot use it because it will always return the same index if the element is not added to manager.
|
||||
*
|
||||
* This function return a index generator that can "remember" the index it generated without actually adding the element to the manager.
|
||||
*
|
||||
* @note The generator cannot work with `group` element.
|
||||
*
|
||||
* @returns
|
||||
*/
|
||||
createIndexGenerator() {
|
||||
const manager = new LayerManager(this._doc, this._surface, {
|
||||
watch: false,
|
||||
});
|
||||
|
||||
return () => {
|
||||
const idx = manager.generateIndex();
|
||||
const bound = new Bound(0, 0, 10, 10);
|
||||
|
||||
const mockedFakeElement = {
|
||||
index: idx,
|
||||
type: 'shape',
|
||||
x: 0,
|
||||
y: 0,
|
||||
w: 10,
|
||||
h: 10,
|
||||
elementBound: bound,
|
||||
xywh: '[0, 0, 10, 10]',
|
||||
get group() {
|
||||
return null;
|
||||
},
|
||||
get groups() {
|
||||
return [];
|
||||
},
|
||||
};
|
||||
|
||||
manager.add(mockedFakeElement as unknown as GfxModel);
|
||||
|
||||
return idx;
|
||||
};
|
||||
}
|
||||
|
||||
delete(element: GfxModel | GfxLocalElementModel) {
|
||||
let deleteType: 'canvas' | 'block' | undefined = undefined;
|
||||
const isGroup = isGfxGroupCompatibleModel(element);
|
||||
const isLocalElem = element instanceof GfxLocalElementModel;
|
||||
|
||||
if (isGroup) {
|
||||
this._reset();
|
||||
this.slots.layerUpdated.emit({
|
||||
type: 'delete',
|
||||
initiatingElement: element as GfxModel,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
element instanceof GfxPrimitiveElementModel ||
|
||||
element instanceof GfxLocalElementModel
|
||||
) {
|
||||
deleteType = 'canvas';
|
||||
if (!isLocalElem) {
|
||||
removeFromOrderedArray(this.canvasElements, element);
|
||||
}
|
||||
} else {
|
||||
deleteType = 'block';
|
||||
removeFromOrderedArray(this.blocks, element);
|
||||
}
|
||||
|
||||
this._removeFromLayer(element, deleteType);
|
||||
|
||||
this._buildCanvasLayers();
|
||||
this.slots.layerUpdated.emit({
|
||||
type: 'delete',
|
||||
initiatingElement: element,
|
||||
});
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.slots.layerUpdated.dispose();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param reverse - if true, generate the index in reverse order
|
||||
* @returns
|
||||
*/
|
||||
generateIndex(reverse = false): string {
|
||||
if (reverse) {
|
||||
const firstIndex = this.layers[0]?.indexes[0];
|
||||
|
||||
return firstIndex
|
||||
? generateKeyBetween(null, ungroupIndex(firstIndex))
|
||||
: LayerManager.INITIAL_INDEX;
|
||||
} else {
|
||||
const lastIndex = last(this.layers)?.indexes[1];
|
||||
|
||||
return lastIndex
|
||||
? generateKeyBetween(ungroupIndex(lastIndex), null)
|
||||
: LayerManager.INITIAL_INDEX;
|
||||
}
|
||||
}
|
||||
|
||||
getCanvasLayers() {
|
||||
return this.canvasLayers;
|
||||
}
|
||||
|
||||
getReorderedIndex(element: GfxModel, direction: ReorderingDirection): string {
|
||||
const group = (element.group as GfxGroupCompatibleInterface) || null;
|
||||
|
||||
let elements: GfxModel[];
|
||||
|
||||
if (group !== null) {
|
||||
elements = group.childElements;
|
||||
|
||||
elements.sort(compare);
|
||||
} else {
|
||||
elements = this.layers.reduce(
|
||||
(pre: GfxModel[], current) =>
|
||||
pre.concat(current.elements.filter(element => element.group == null)),
|
||||
[]
|
||||
);
|
||||
}
|
||||
|
||||
const currentIdx = elements.indexOf(element);
|
||||
|
||||
switch (direction) {
|
||||
case 'forward':
|
||||
case 'front':
|
||||
if (currentIdx === -1 || currentIdx === elements.length - 1)
|
||||
return element.index;
|
||||
|
||||
{
|
||||
const next =
|
||||
direction === 'forward'
|
||||
? elements[currentIdx + 1]
|
||||
: elements[elements.length - 1];
|
||||
const next2 =
|
||||
direction === 'forward' ? elements[currentIdx + 2] : null;
|
||||
|
||||
return generateKeyBetween(
|
||||
next.index,
|
||||
next2?.index
|
||||
? next.index < next2.index
|
||||
? next2.index
|
||||
: null
|
||||
: null
|
||||
);
|
||||
}
|
||||
case 'backward':
|
||||
case 'back':
|
||||
if (currentIdx === -1 || currentIdx === 0) return element.index;
|
||||
|
||||
{
|
||||
const pre =
|
||||
direction === 'backward' ? elements[currentIdx - 1] : elements[0];
|
||||
const pre2 =
|
||||
direction === 'backward' ? elements[currentIdx - 2] : null;
|
||||
|
||||
return generateKeyBetween(
|
||||
!pre2 || pre2?.index >= pre.index ? null : pre2.index,
|
||||
pre.index
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
getZIndex(element: GfxModel): number {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
const layer = this.layers.find(layer => layer.set.has(element));
|
||||
|
||||
if (!layer) return -1;
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
return layer.zIndex + layer.elements.indexOf(element);
|
||||
}
|
||||
|
||||
update(
|
||||
element: GfxModel | GfxLocalElementModel,
|
||||
props?: Record<string, unknown>
|
||||
) {
|
||||
if (this._updateLayer(element, props)) {
|
||||
this._buildCanvasLayers();
|
||||
this.slots.layerUpdated.emit({
|
||||
type: 'update',
|
||||
initiatingElement: element,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
watch(blocks: { doc?: Doc; surface: SurfaceBlockModel | null }) {
|
||||
const { doc, surface } = blocks;
|
||||
|
||||
if (doc) {
|
||||
this._disposable.add(
|
||||
doc.slots.blockUpdated.on(payload => {
|
||||
if (payload.type === 'add') {
|
||||
const block = doc.getBlockById(payload.id)!;
|
||||
|
||||
if (
|
||||
block instanceof GfxBlockElementModel &&
|
||||
(block.parent instanceof SurfaceBlockModel ||
|
||||
block.parent?.role === 'root') &&
|
||||
this.blocks.indexOf(block) === -1
|
||||
) {
|
||||
this.add(block as GfxBlockElementModel);
|
||||
}
|
||||
}
|
||||
if (payload.type === 'update') {
|
||||
const block = doc.getBlockById(payload.id)!;
|
||||
|
||||
if (
|
||||
(payload.props.key === 'index' ||
|
||||
payload.props.key === 'childIds') &&
|
||||
block instanceof GfxBlockElementModel &&
|
||||
(block.parent instanceof SurfaceBlockModel ||
|
||||
block.parent?.role === 'root')
|
||||
) {
|
||||
this.update(block as GfxBlockElementModel, {
|
||||
[payload.props.key]: true,
|
||||
});
|
||||
} else if (
|
||||
this.blocks.includes(block as GfxBlockElementModel) &&
|
||||
!(
|
||||
block.parent instanceof SurfaceBlockModel ||
|
||||
block.parent?.role === 'root'
|
||||
)
|
||||
) {
|
||||
this.delete(block as GfxBlockElementModel);
|
||||
}
|
||||
}
|
||||
if (payload.type === 'delete') {
|
||||
const block = doc.getBlockById(payload.id);
|
||||
|
||||
if (block instanceof GfxBlockElementModel) {
|
||||
this.delete(block as GfxBlockElementModel);
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (surface) {
|
||||
if (this._surface !== surface) {
|
||||
this._surface = surface;
|
||||
}
|
||||
|
||||
this._disposable.add(
|
||||
surface.elementAdded.on(payload =>
|
||||
this.add(surface.getElementById(payload.id)!)
|
||||
)
|
||||
);
|
||||
this._disposable.add(
|
||||
surface.elementUpdated.on(payload => {
|
||||
if (payload.props['index'] || payload.props['childIds']) {
|
||||
this.update(surface.getElementById(payload.id)!, payload.props);
|
||||
}
|
||||
})
|
||||
);
|
||||
this._disposable.add(
|
||||
surface.elementRemoved.on(payload => this.delete(payload.model!))
|
||||
);
|
||||
this._disposable.add(
|
||||
surface.localElementAdded.on(elm => {
|
||||
this.add(elm);
|
||||
})
|
||||
);
|
||||
this._disposable.add(
|
||||
this._surface.localElementUpdated.on(payload => {
|
||||
if (payload.props['index'] || payload.props['groupId']) {
|
||||
this.update(payload.model, payload.props);
|
||||
}
|
||||
})
|
||||
);
|
||||
this._disposable.add(
|
||||
surface.localElementDeleted.on(elm => {
|
||||
this.delete(elm);
|
||||
})
|
||||
);
|
||||
|
||||
surface.elementModels.forEach(el => this.add(el));
|
||||
}
|
||||
}
|
||||
}
|
||||
167
blocksuite/framework/block-std/src/gfx/model/base.ts
Normal file
167
blocksuite/framework/block-std/src/gfx/model/base.ts
Normal file
@@ -0,0 +1,167 @@
|
||||
import type {
|
||||
Bound,
|
||||
IBound,
|
||||
IVec,
|
||||
PointLocation,
|
||||
SerializedXYWH,
|
||||
XYWH,
|
||||
} from '@blocksuite/global/utils';
|
||||
|
||||
import type { EditorHost } from '../../view/element/lit-host.js';
|
||||
import type { GfxGroupModel, GfxModel } from './model.js';
|
||||
|
||||
/**
|
||||
* The methods that a graphic element should implement.
|
||||
* It is already included in the `GfxCompatibleInterface` interface.
|
||||
*/
|
||||
export interface GfxElementGeometry {
|
||||
containsBound(bound: Bound): boolean;
|
||||
getNearestPoint(point: IVec): IVec;
|
||||
getLineIntersections(start: IVec, end: IVec): PointLocation[] | null;
|
||||
getRelativePointLocation(point: IVec): PointLocation;
|
||||
includesPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
options: PointTestOptions,
|
||||
host: EditorHost
|
||||
): boolean;
|
||||
intersectsBound(bound: Bound): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* All the model that can be rendered in graphics mode should implement this interface.
|
||||
*/
|
||||
export interface GfxCompatibleInterface extends IBound, GfxElementGeometry {
|
||||
xywh: SerializedXYWH;
|
||||
index: string;
|
||||
|
||||
/**
|
||||
* Defines the extension of the response area beyond the element's bounding box.
|
||||
* This tuple specifies the horizontal and vertical margins to be added to the element's bound.
|
||||
*
|
||||
* The first value represents the horizontal extension (added to both left and right sides),
|
||||
* and the second value represents the vertical extension (added to both top and bottom sides).
|
||||
*
|
||||
* The response area is computed as:
|
||||
* `[x - horizontal, y - vertical, w + 2 * horizontal, h + 2 * vertical]`.
|
||||
*
|
||||
* Example:
|
||||
* - xywh: `[0, 0, 100, 100]`, `responseExtension: [10, 20]`
|
||||
* Resulting response area: `[-10, -20, 120, 140]`.
|
||||
* - `responseExtension: [0, 0]` keeps the response area equal to the bounding box.
|
||||
*/
|
||||
responseExtension: [number, number];
|
||||
|
||||
readonly group: GfxGroupCompatibleInterface | null;
|
||||
|
||||
readonly groups: GfxGroupCompatibleInterface[];
|
||||
|
||||
readonly deserializedXYWH: XYWH;
|
||||
|
||||
/**
|
||||
* The bound of the element without considering the response extension.
|
||||
*/
|
||||
readonly elementBound: Bound;
|
||||
|
||||
/**
|
||||
* The bound of the element considering the response extension.
|
||||
*/
|
||||
readonly responseBound: Bound;
|
||||
|
||||
/**
|
||||
* Indicates whether the current block is explicitly locked by self.
|
||||
* For checking the lock status of the element, use `isLocked` instead.
|
||||
* For (un)locking the element, use `(un)lock` instead.
|
||||
*/
|
||||
lockedBySelf?: boolean;
|
||||
|
||||
/**
|
||||
* Check if the element is locked. It will check the lock status of the element and its ancestors.
|
||||
*/
|
||||
isLocked(): boolean;
|
||||
isLockedBySelf(): boolean;
|
||||
isLockedByAncestor(): boolean;
|
||||
|
||||
lock(): void;
|
||||
unlock(): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* The symbol to mark a model as a container.
|
||||
*/
|
||||
export const gfxGroupCompatibleSymbol = Symbol('GfxGroupCompatible');
|
||||
|
||||
/**
|
||||
* Check if the element is a container element.
|
||||
*/
|
||||
export const isGfxGroupCompatibleModel = (
|
||||
elm: unknown
|
||||
): elm is GfxGroupModel => {
|
||||
if (typeof elm !== 'object' || elm === null) return false;
|
||||
return (
|
||||
gfxGroupCompatibleSymbol in elm && elm[gfxGroupCompatibleSymbol] === true
|
||||
);
|
||||
};
|
||||
|
||||
/**
|
||||
* GfxGroupCompatibleElement is a model that can contain other models.
|
||||
* It just like a group that in common graphic software.
|
||||
*/
|
||||
export interface GfxGroupCompatibleInterface extends GfxCompatibleInterface {
|
||||
[gfxGroupCompatibleSymbol]: true;
|
||||
|
||||
/**
|
||||
* All child ids of this container.
|
||||
*/
|
||||
childIds: string[];
|
||||
|
||||
/**
|
||||
* All child element models of this container.
|
||||
* Note that the `childElements` may not contains all the children in `childIds`,
|
||||
* because some children may not be loaded.
|
||||
*/
|
||||
childElements: GfxModel[];
|
||||
|
||||
descendantElements: GfxModel[];
|
||||
|
||||
addChild(element: GfxCompatibleInterface): void;
|
||||
removeChild(element: GfxCompatibleInterface): void;
|
||||
hasChild(element: GfxCompatibleInterface): boolean;
|
||||
|
||||
hasDescendant(element: GfxCompatibleInterface): boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* The options for the hit testing of a point.
|
||||
*/
|
||||
export interface PointTestOptions {
|
||||
/**
|
||||
* The threshold of the hit test. The unit is pixel.
|
||||
*/
|
||||
hitThreshold?: number;
|
||||
|
||||
/**
|
||||
* If true, the element bound will be used for the hit testing.
|
||||
* By default, the response bound will be used.
|
||||
*/
|
||||
useElementBound?: boolean;
|
||||
|
||||
/**
|
||||
* The padding of the response area for each element when do the hit testing. The unit is pixel.
|
||||
* The first value is the padding for the x-axis, and the second value is the padding for the y-axis.
|
||||
*/
|
||||
responsePadding?: [number, number];
|
||||
|
||||
/**
|
||||
* If true, the transparent area of the element will be ignored during the point inclusion test.
|
||||
* Otherwise, the transparent area will be considered as filled area.
|
||||
*
|
||||
* Default is true.
|
||||
*/
|
||||
ignoreTransparent?: boolean;
|
||||
|
||||
/**
|
||||
* The zoom level of current view when do the hit testing.
|
||||
*/
|
||||
zoom?: number;
|
||||
}
|
||||
271
blocksuite/framework/block-std/src/gfx/model/gfx-block-model.ts
Normal file
271
blocksuite/framework/block-std/src/gfx/model/gfx-block-model.ts
Normal file
@@ -0,0 +1,271 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import type {
|
||||
Constructor,
|
||||
IVec,
|
||||
SerializedXYWH,
|
||||
XYWH,
|
||||
} from '@blocksuite/global/utils';
|
||||
import {
|
||||
Bound,
|
||||
deserializeXYWH,
|
||||
getBoundWithRotation,
|
||||
getPointsFromBoundWithRotation,
|
||||
linePolygonIntersects,
|
||||
PointLocation,
|
||||
polygonGetPointTangent,
|
||||
polygonNearestPoint,
|
||||
rotatePoints,
|
||||
} from '@blocksuite/global/utils';
|
||||
import { BlockModel } from '@blocksuite/store';
|
||||
|
||||
import {
|
||||
isLockedByAncestorImpl,
|
||||
isLockedBySelfImpl,
|
||||
isLockedImpl,
|
||||
lockElementImpl,
|
||||
unlockElementImpl,
|
||||
} from '../../utils/tree.js';
|
||||
import type { EditorHost } from '../../view/index.js';
|
||||
import type { GfxCompatibleInterface, PointTestOptions } from './base.js';
|
||||
import type { GfxGroupModel } from './model.js';
|
||||
import type { SurfaceBlockModel } from './surface/surface-model.js';
|
||||
|
||||
/**
|
||||
* The props that a graphics block model should have.
|
||||
*/
|
||||
export type GfxCompatibleProps = {
|
||||
xywh: SerializedXYWH;
|
||||
index: string;
|
||||
lockedBySelf?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
* This type include the common props for the graphic block model.
|
||||
* You can use this type with Omit to define the props of a graphic block model.
|
||||
*/
|
||||
export type GfxCommonBlockProps = GfxCompatibleProps & {
|
||||
rotate: number;
|
||||
scale: number;
|
||||
};
|
||||
|
||||
/**
|
||||
* The graphic block model that can be rendered in the graphics mode.
|
||||
* All the graphic block model should extend this class.
|
||||
* You can use `GfxCompatibleBlockModel` to convert a BlockModel to a subclass that extends it.
|
||||
*/
|
||||
export class GfxBlockElementModel<
|
||||
Props extends GfxCompatibleProps = GfxCompatibleProps,
|
||||
>
|
||||
extends BlockModel<Props>
|
||||
implements GfxCompatibleInterface
|
||||
{
|
||||
private _cacheDeserKey: string | null = null;
|
||||
|
||||
private _cacheDeserXYWH: XYWH | null = null;
|
||||
|
||||
private _externalXYWH: SerializedXYWH | undefined = undefined;
|
||||
|
||||
connectable = true;
|
||||
|
||||
/**
|
||||
* Defines the extension of the response area beyond the element's bounding box.
|
||||
* This tuple specifies the horizontal and vertical margins to be added to the element's [x, y, width, height].
|
||||
*
|
||||
* The first value represents the horizontal extension (added to both left and right sides),
|
||||
* and the second value represents the vertical extension (added to both top and bottom sides).
|
||||
*
|
||||
* The response area is computed as:
|
||||
* `[x - horizontal, y - vertical, width + 2 * horizontal, height + 2 * vertical]`.
|
||||
*
|
||||
* Example:
|
||||
* - Bounding box: `[0, 0, 100, 100]`, `responseExtension: [10, 20]`
|
||||
* Resulting response area: `[-10, -20, 120, 140]`.
|
||||
* - `responseExtension: [0, 0]` keeps the response area equal to the bounding box.
|
||||
*/
|
||||
responseExtension: [number, number] = [0, 0];
|
||||
|
||||
rotate = 0;
|
||||
|
||||
get deserializedXYWH() {
|
||||
if (this._cacheDeserKey !== this.xywh || !this._cacheDeserXYWH) {
|
||||
this._cacheDeserKey = this.xywh;
|
||||
this._cacheDeserXYWH = deserializeXYWH(this.xywh);
|
||||
}
|
||||
|
||||
return this._cacheDeserXYWH;
|
||||
}
|
||||
|
||||
get elementBound() {
|
||||
return Bound.from(getBoundWithRotation(this));
|
||||
}
|
||||
|
||||
get externalBound(): Bound | null {
|
||||
return this._externalXYWH ? Bound.deserialize(this._externalXYWH) : null;
|
||||
}
|
||||
|
||||
get externalXYWH(): SerializedXYWH | undefined {
|
||||
return this._externalXYWH;
|
||||
}
|
||||
|
||||
set externalXYWH(xywh: SerializedXYWH | undefined) {
|
||||
this._externalXYWH = xywh;
|
||||
}
|
||||
|
||||
get group(): GfxGroupModel | null {
|
||||
if (!this.surface) return null;
|
||||
|
||||
return this.surface.getGroup(this.id) ?? null;
|
||||
}
|
||||
|
||||
get groups(): GfxGroupModel[] {
|
||||
if (!this.surface) return [];
|
||||
|
||||
return this.surface.getGroups(this.id);
|
||||
}
|
||||
|
||||
get h() {
|
||||
return this.deserializedXYWH[3];
|
||||
}
|
||||
|
||||
get responseBound() {
|
||||
return this.elementBound.expand(this.responseExtension);
|
||||
}
|
||||
|
||||
get surface(): SurfaceBlockModel | null {
|
||||
const result = this.doc.getBlocksByFlavour('affine:surface');
|
||||
if (result.length === 0) return null;
|
||||
return result[0].model as SurfaceBlockModel;
|
||||
}
|
||||
|
||||
get w() {
|
||||
return this.deserializedXYWH[2];
|
||||
}
|
||||
|
||||
get x() {
|
||||
return this.deserializedXYWH[0];
|
||||
}
|
||||
|
||||
get y() {
|
||||
return this.deserializedXYWH[1];
|
||||
}
|
||||
|
||||
containsBound(bounds: Bound): boolean {
|
||||
const bound = Bound.deserialize(this.xywh);
|
||||
const points = getPointsFromBoundWithRotation({
|
||||
x: bound.x,
|
||||
y: bound.y,
|
||||
w: bound.w,
|
||||
h: bound.h,
|
||||
rotate: this.rotate,
|
||||
});
|
||||
return points.some(point => bounds.containsPoint(point));
|
||||
}
|
||||
|
||||
getLineIntersections(start: IVec, end: IVec): PointLocation[] | null {
|
||||
const bound = Bound.deserialize(this.xywh);
|
||||
|
||||
return linePolygonIntersects(
|
||||
start,
|
||||
end,
|
||||
rotatePoints(bound.points, bound.center, this.rotate ?? 0)
|
||||
);
|
||||
}
|
||||
|
||||
getNearestPoint(point: IVec): IVec {
|
||||
const bound = Bound.deserialize(this.xywh);
|
||||
return polygonNearestPoint(
|
||||
rotatePoints(bound.points, bound.center, this.rotate ?? 0),
|
||||
point
|
||||
);
|
||||
}
|
||||
|
||||
getRelativePointLocation(relativePoint: IVec): PointLocation {
|
||||
const bound = Bound.deserialize(this.xywh);
|
||||
const point = bound.getRelativePoint(relativePoint);
|
||||
const rotatePoint = rotatePoints(
|
||||
[point],
|
||||
bound.center,
|
||||
this.rotate ?? 0
|
||||
)[0];
|
||||
const points = rotatePoints(bound.points, bound.center, this.rotate ?? 0);
|
||||
const tangent = polygonGetPointTangent(points, rotatePoint);
|
||||
|
||||
return new PointLocation(rotatePoint, tangent);
|
||||
}
|
||||
|
||||
includesPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
opt: PointTestOptions,
|
||||
__: EditorHost
|
||||
): boolean {
|
||||
const bound = opt.useElementBound ? this.elementBound : this.responseBound;
|
||||
return bound.isPointInBound([x, y], 0);
|
||||
}
|
||||
|
||||
intersectsBound(bound: Bound): boolean {
|
||||
return (
|
||||
this.containsBound(bound) ||
|
||||
bound.points.some((point, i, points) =>
|
||||
this.getLineIntersections(point, points[(i + 1) % points.length])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
isLocked(): boolean {
|
||||
return isLockedImpl(this);
|
||||
}
|
||||
|
||||
isLockedByAncestor(): boolean {
|
||||
return isLockedByAncestorImpl(this);
|
||||
}
|
||||
|
||||
isLockedBySelf(): boolean {
|
||||
return isLockedBySelfImpl(this);
|
||||
}
|
||||
|
||||
lock() {
|
||||
lockElementImpl(this.doc, this);
|
||||
}
|
||||
|
||||
unlock() {
|
||||
unlockElementImpl(this.doc, this);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a BlockModel to a GfxBlockElementModel.
|
||||
* @param BlockModelSuperClass The BlockModel class to be converted.
|
||||
* @returns The returned class is a subclass of the GfxBlockElementModel class and the given BlockModelSuperClass.
|
||||
*/
|
||||
export function GfxCompatibleBlockModel<
|
||||
Props extends GfxCompatibleProps,
|
||||
T extends Constructor<BlockModel<Props>> = Constructor<BlockModel<Props>>,
|
||||
>(BlockModelSuperClass: T) {
|
||||
if (BlockModelSuperClass === BlockModel) {
|
||||
return GfxBlockElementModel as unknown as typeof GfxBlockElementModel<Props>;
|
||||
} else {
|
||||
let currentClass = BlockModelSuperClass;
|
||||
|
||||
while (
|
||||
Object.getPrototypeOf(currentClass.prototype) !== BlockModel.prototype &&
|
||||
Object.getPrototypeOf(currentClass.prototype) !== null
|
||||
) {
|
||||
currentClass = Object.getPrototypeOf(currentClass.prototype).constructor;
|
||||
}
|
||||
|
||||
if (Object.getPrototypeOf(currentClass.prototype) === null) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.GfxBlockElementError,
|
||||
'The SuperClass is not a subclass of BlockModel'
|
||||
);
|
||||
}
|
||||
|
||||
Object.setPrototypeOf(
|
||||
currentClass.prototype,
|
||||
GfxBlockElementModel.prototype
|
||||
);
|
||||
}
|
||||
|
||||
return BlockModelSuperClass as unknown as typeof GfxBlockElementModel<Props>;
|
||||
}
|
||||
12
blocksuite/framework/block-std/src/gfx/model/model.ts
Normal file
12
blocksuite/framework/block-std/src/gfx/model/model.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import type { GfxGroupCompatibleInterface } from './base.js';
|
||||
import type { GfxBlockElementModel } from './gfx-block-model.js';
|
||||
import type {
|
||||
GfxGroupLikeElementModel,
|
||||
GfxPrimitiveElementModel,
|
||||
} from './surface/element-model.js';
|
||||
|
||||
export type GfxModel = GfxBlockElementModel | GfxPrimitiveElementModel;
|
||||
|
||||
export type GfxGroupModel =
|
||||
| (GfxGroupCompatibleInterface & GfxBlockElementModel)
|
||||
| GfxGroupLikeElementModel;
|
||||
@@ -0,0 +1,53 @@
|
||||
import type { SurfaceBlockModel } from '../surface-model.js';
|
||||
|
||||
/**
|
||||
* Set metadata for a property
|
||||
* @param symbol Unique symbol for the metadata
|
||||
* @param target The target object to set metadata on, usually the prototype
|
||||
* @param prop The property name
|
||||
* @param val The value to set
|
||||
*/
|
||||
export function setObjectPropMeta(
|
||||
symbol: symbol,
|
||||
target: unknown,
|
||||
prop: string | symbol,
|
||||
val: unknown
|
||||
) {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
target[symbol] = target[symbol] ?? {};
|
||||
// @ts-expect-error FIXME: ts error
|
||||
target[symbol][prop] = val;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get metadata for a property
|
||||
* @param target The target object to retrieve metadata from, usually the prototype
|
||||
* @param symbol Unique symbol for the metadata
|
||||
* @param prop The property name, if not provided, returns all metadata for that symbol
|
||||
* @returns
|
||||
*/
|
||||
export function getObjectPropMeta(
|
||||
target: unknown,
|
||||
symbol: symbol,
|
||||
prop?: string | symbol
|
||||
) {
|
||||
if (prop) {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
return target[symbol]?.[prop] ?? null;
|
||||
}
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
return target[symbol] ?? {};
|
||||
}
|
||||
|
||||
export function getDecoratorState(surface: SurfaceBlockModel) {
|
||||
return surface['_decoratorState'];
|
||||
}
|
||||
|
||||
export function createDecoratorState() {
|
||||
return {
|
||||
creating: false,
|
||||
deriving: false,
|
||||
skipField: false,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import type { GfxPrimitiveElementModel } from '../element-model.js';
|
||||
import { getObjectPropMeta, setObjectPropMeta } from './common.js';
|
||||
|
||||
const convertSymbol = Symbol('convert');
|
||||
|
||||
/**
|
||||
* The convert decorator is used to convert the property value before it's
|
||||
* set to the Y map.
|
||||
*
|
||||
* Note:
|
||||
* 1. This decorator function will not execute in model initialization.
|
||||
* @param fn
|
||||
* @returns
|
||||
*/
|
||||
export function convert<V, T extends GfxPrimitiveElementModel>(
|
||||
fn: (propValue: V, instance: T) => unknown
|
||||
) {
|
||||
return function convertDecorator(
|
||||
_: unknown,
|
||||
context: ClassAccessorDecoratorContext
|
||||
) {
|
||||
const prop = String(context.name);
|
||||
return {
|
||||
init(this: T, v: V) {
|
||||
const proto = Object.getPrototypeOf(this);
|
||||
setObjectPropMeta(convertSymbol, proto, prop, fn);
|
||||
return v;
|
||||
},
|
||||
} as ClassAccessorDecoratorResult<T, V>;
|
||||
};
|
||||
}
|
||||
|
||||
function getConvertMeta(
|
||||
proto: unknown,
|
||||
prop: string | symbol
|
||||
): null | ((propValue: unknown, instance: unknown) => unknown) {
|
||||
return getObjectPropMeta(proto, convertSymbol, prop);
|
||||
}
|
||||
|
||||
export function convertProps(
|
||||
propName: string | symbol,
|
||||
propValue: unknown,
|
||||
receiver: unknown
|
||||
) {
|
||||
const proto = Object.getPrototypeOf(receiver);
|
||||
const convertFn = getConvertMeta(proto, propName as string)!;
|
||||
|
||||
return convertFn ? convertFn(propValue, receiver) : propValue;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import type { GfxPrimitiveElementModel } from '../element-model.js';
|
||||
import {
|
||||
getDecoratorState,
|
||||
getObjectPropMeta,
|
||||
setObjectPropMeta,
|
||||
} from './common.js';
|
||||
|
||||
const deriveSymbol = Symbol('derive');
|
||||
|
||||
const keys = Object.keys;
|
||||
|
||||
function getDerivedMeta(
|
||||
proto: unknown,
|
||||
prop: string | symbol
|
||||
):
|
||||
| null
|
||||
| ((propValue: unknown, instance: unknown) => Record<string, unknown>)[] {
|
||||
return getObjectPropMeta(proto, deriveSymbol, prop);
|
||||
}
|
||||
|
||||
export function getDerivedProps(
|
||||
prop: string | symbol,
|
||||
propValue: unknown,
|
||||
receiver: GfxPrimitiveElementModel
|
||||
) {
|
||||
const prototype = Object.getPrototypeOf(receiver);
|
||||
const decoratorState = getDecoratorState(receiver.surface);
|
||||
|
||||
if (decoratorState.deriving || decoratorState.creating) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const deriveFns = getDerivedMeta(prototype, prop as string)!;
|
||||
|
||||
return deriveFns
|
||||
? deriveFns.reduce(
|
||||
(derivedProps, fn) => {
|
||||
const props = fn(propValue, receiver);
|
||||
|
||||
Object.entries(props).forEach(([key, value]) => {
|
||||
derivedProps[key] = value;
|
||||
});
|
||||
|
||||
return derivedProps;
|
||||
},
|
||||
{} as Record<string, unknown>
|
||||
)
|
||||
: null;
|
||||
}
|
||||
|
||||
export function updateDerivedProps(
|
||||
derivedProps: Record<string, unknown> | null,
|
||||
receiver: GfxPrimitiveElementModel
|
||||
) {
|
||||
if (derivedProps) {
|
||||
const decoratorState = getDecoratorState(receiver.surface);
|
||||
decoratorState.deriving = true;
|
||||
keys(derivedProps).forEach(key => {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
receiver[key] = derivedProps[key];
|
||||
});
|
||||
decoratorState.deriving = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The derive decorator is used to derive other properties' update when the
|
||||
* decorated property is updated through assignment in the local.
|
||||
*
|
||||
* Note:
|
||||
* 1. The first argument of the function is the new value of the decorated property
|
||||
* before the `convert` decorator is called.
|
||||
* 2. The decorator function will execute after the decorated property has been updated.
|
||||
* 3. The decorator function will not execute during model creation.
|
||||
* 4. The decorator function will not execute if the decorated property is updated through
|
||||
* the Y map. That is to say, if other peers update the property will not trigger this decorator
|
||||
* @param fn
|
||||
* @returns
|
||||
*/
|
||||
export function derive<V, T extends GfxPrimitiveElementModel>(
|
||||
fn: (propValue: any, instance: T) => Record<string, unknown>
|
||||
) {
|
||||
return function deriveDecorator(
|
||||
_: unknown,
|
||||
context: ClassAccessorDecoratorContext
|
||||
) {
|
||||
const prop = String(context.name);
|
||||
return {
|
||||
init(this: GfxPrimitiveElementModel, v: V) {
|
||||
const proto = Object.getPrototypeOf(this);
|
||||
const derived = getDerivedMeta(proto, prop);
|
||||
|
||||
if (Array.isArray(derived)) {
|
||||
derived.push(fn as (typeof derived)[0]);
|
||||
} else {
|
||||
setObjectPropMeta(deriveSymbol, proto, prop as string, [fn]);
|
||||
}
|
||||
|
||||
return v;
|
||||
},
|
||||
} as ClassAccessorDecoratorResult<GfxPrimitiveElementModel, V>;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
import type { GfxPrimitiveElementModel } from '../element-model.js';
|
||||
import { getDecoratorState } from './common.js';
|
||||
import { convertProps } from './convert.js';
|
||||
import { getDerivedProps, updateDerivedProps } from './derive.js';
|
||||
import { startObserve } from './observer.js';
|
||||
|
||||
const yPropsSetSymbol = Symbol('yProps');
|
||||
|
||||
export function getFieldPropsSet(target: unknown): Set<string | symbol> {
|
||||
const proto = Object.getPrototypeOf(target);
|
||||
if (!Object.hasOwn(proto, yPropsSetSymbol)) {
|
||||
proto[yPropsSetSymbol] = new Set();
|
||||
}
|
||||
|
||||
return proto[yPropsSetSymbol] as Set<string | symbol>;
|
||||
}
|
||||
|
||||
export function field<V, T extends GfxPrimitiveElementModel>(fallback?: V) {
|
||||
return function yDecorator(
|
||||
_: ClassAccessorDecoratorTarget<T, V>,
|
||||
context: ClassAccessorDecoratorContext
|
||||
) {
|
||||
const prop = context.name;
|
||||
|
||||
return {
|
||||
init(this: GfxPrimitiveElementModel, v: V) {
|
||||
const yProps = getFieldPropsSet(this);
|
||||
|
||||
yProps.add(prop);
|
||||
|
||||
if (
|
||||
getDecoratorState(
|
||||
this.surface ?? Object.getPrototypeOf(this).constructor
|
||||
)?.skipField
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.yMap) {
|
||||
if (this.yMap.doc) {
|
||||
this.surface.doc.transact(() => {
|
||||
this.yMap.set(prop as string, v);
|
||||
});
|
||||
} else {
|
||||
this.yMap.set(prop as string, v);
|
||||
this._preserved.set(prop as string, v);
|
||||
}
|
||||
}
|
||||
|
||||
return v;
|
||||
},
|
||||
get(this: GfxPrimitiveElementModel) {
|
||||
return (
|
||||
(this.yMap.doc ? this.yMap.get(prop as string) : null) ??
|
||||
this._preserved.get(prop as string) ??
|
||||
fallback
|
||||
);
|
||||
},
|
||||
set(this: T, originalVal: V) {
|
||||
const isCreating = getDecoratorState(this.surface)?.creating;
|
||||
|
||||
if (getDecoratorState(this.surface)?.skipField) {
|
||||
return;
|
||||
}
|
||||
|
||||
const derivedProps = getDerivedProps(prop, originalVal, this);
|
||||
const val = isCreating
|
||||
? originalVal
|
||||
: convertProps(prop, originalVal, this);
|
||||
|
||||
if (this.yMap.doc) {
|
||||
this.surface.doc.transact(() => {
|
||||
this.yMap.set(prop as string, val);
|
||||
});
|
||||
} else {
|
||||
this.yMap.set(prop as string, val);
|
||||
this._preserved.set(prop as string, val);
|
||||
}
|
||||
|
||||
startObserve(prop as string, this);
|
||||
|
||||
if (!isCreating) {
|
||||
updateDerivedProps(derivedProps, this);
|
||||
}
|
||||
},
|
||||
} as ClassAccessorDecoratorResult<T, V>;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export { convert, convertProps } from './convert.js';
|
||||
export { derive, getDerivedProps, updateDerivedProps } from './derive.js';
|
||||
export { field, getFieldPropsSet } from './field.js';
|
||||
export { local } from './local.js';
|
||||
export { initializeObservers, observe } from './observer.js';
|
||||
export { initializeWatchers, watch } from './watch.js';
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { GfxPrimitiveElementModel } from '../element-model.js';
|
||||
import { getDecoratorState } from './common.js';
|
||||
import { convertProps } from './convert.js';
|
||||
import { getDerivedProps, updateDerivedProps } from './derive.js';
|
||||
|
||||
/**
|
||||
* A decorator to mark the property as a local property.
|
||||
*
|
||||
* The local property act like it is a field property, but it's not synced to the Y map.
|
||||
* Updating local property will also trigger the `elementUpdated` slot of the surface model
|
||||
*/
|
||||
export function local<V, T extends GfxPrimitiveElementModel>() {
|
||||
return function localDecorator(
|
||||
_target: ClassAccessorDecoratorTarget<T, V>,
|
||||
context: ClassAccessorDecoratorContext
|
||||
) {
|
||||
const prop = context.name;
|
||||
|
||||
return {
|
||||
init(this: T, v: V) {
|
||||
this._local.set(prop, v);
|
||||
|
||||
return v;
|
||||
},
|
||||
get(this: T) {
|
||||
return this._local.get(prop);
|
||||
},
|
||||
set(this: T, originalValue: unknown) {
|
||||
const isCreating = getDecoratorState(this.surface)?.creating;
|
||||
const oldValue = this._local.get(prop);
|
||||
// When state is creating, the value is considered as default value
|
||||
// hence there's no need to convert it
|
||||
const newVal = isCreating
|
||||
? originalValue
|
||||
: convertProps(prop, originalValue, this);
|
||||
|
||||
const derivedProps = getDerivedProps(prop, originalValue, this);
|
||||
|
||||
this._local.set(prop, newVal);
|
||||
|
||||
// During creating, no need to invoke an update event and derive another update
|
||||
if (!isCreating) {
|
||||
updateDerivedProps(derivedProps, this);
|
||||
|
||||
this._onChange({
|
||||
props: {
|
||||
[prop]: newVal,
|
||||
},
|
||||
oldValues: {
|
||||
[prop]: oldValue,
|
||||
},
|
||||
local: true,
|
||||
});
|
||||
}
|
||||
},
|
||||
} as ClassAccessorDecoratorResult<T, V>;
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
import type { Y } from '@blocksuite/store';
|
||||
|
||||
import type { GfxPrimitiveElementModel } from '../element-model.js';
|
||||
import { getObjectPropMeta, setObjectPropMeta } from './common.js';
|
||||
|
||||
const observeSymbol = Symbol('observe');
|
||||
const observerDisposableSymbol = Symbol('observerDisposable');
|
||||
|
||||
type ObserveFn<
|
||||
E extends Y.YEvent<any> = Y.YEvent<any>,
|
||||
T extends GfxPrimitiveElementModel = GfxPrimitiveElementModel,
|
||||
> = (
|
||||
/**
|
||||
* The event object of the Y.Map or Y.Array, the `null` value means the observer is initializing.
|
||||
*/
|
||||
event: E | null,
|
||||
instance: T,
|
||||
/**
|
||||
* The transaction object of the Y.Map or Y.Array, the `null` value means the observer is initializing.
|
||||
*/
|
||||
transaction: Y.Transaction | null
|
||||
) => void;
|
||||
|
||||
/**
|
||||
* A decorator to observe the y type property.
|
||||
* You can think of it is just a decorator version of 'observe' method of Y.Array and Y.Map.
|
||||
*
|
||||
* The observer function start to observe the property when the model is mounted. And it will
|
||||
* re-observe the property automatically when the value is altered.
|
||||
* @param fn
|
||||
* @returns
|
||||
*/
|
||||
export function observe<
|
||||
V,
|
||||
E extends Y.YEvent<any>,
|
||||
T extends GfxPrimitiveElementModel,
|
||||
>(fn: ObserveFn<E, T>) {
|
||||
return function observeDecorator(
|
||||
_: unknown,
|
||||
context: ClassAccessorDecoratorContext
|
||||
) {
|
||||
const prop = context.name;
|
||||
return {
|
||||
init(this: T, v: V) {
|
||||
setObjectPropMeta(observeSymbol, Object.getPrototypeOf(this), prop, fn);
|
||||
return v;
|
||||
},
|
||||
} as ClassAccessorDecoratorResult<GfxPrimitiveElementModel, V>;
|
||||
};
|
||||
}
|
||||
|
||||
function getObserveMeta(
|
||||
proto: unknown,
|
||||
prop: string | symbol
|
||||
): null | ObserveFn {
|
||||
return getObjectPropMeta(proto, observeSymbol, prop);
|
||||
}
|
||||
|
||||
export function startObserve(
|
||||
prop: string | symbol,
|
||||
receiver: GfxPrimitiveElementModel
|
||||
) {
|
||||
const proto = Object.getPrototypeOf(receiver);
|
||||
const observeFn = getObserveMeta(proto, prop as string)!;
|
||||
// @ts-expect-error FIXME: ts error
|
||||
const observerDisposable = receiver[observerDisposableSymbol] ?? {};
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
receiver[observerDisposableSymbol] = observerDisposable;
|
||||
|
||||
if (observerDisposable[prop]) {
|
||||
observerDisposable[prop]();
|
||||
delete observerDisposable[prop];
|
||||
}
|
||||
|
||||
if (!observeFn) {
|
||||
return;
|
||||
}
|
||||
|
||||
const value = receiver[prop as keyof GfxPrimitiveElementModel] as
|
||||
| Y.Map<unknown>
|
||||
| Y.Array<unknown>
|
||||
| null;
|
||||
|
||||
observeFn(null, receiver, null);
|
||||
|
||||
const fn = (event: Y.YEvent<any>, transaction: Y.Transaction) => {
|
||||
observeFn(event, receiver, transaction);
|
||||
};
|
||||
|
||||
if (value && 'observe' in value) {
|
||||
value.observe(fn);
|
||||
|
||||
observerDisposable[prop] = () => {
|
||||
value.unobserve(fn);
|
||||
};
|
||||
} else {
|
||||
console.warn(
|
||||
`Failed to observe "${prop.toString()}" of ${
|
||||
receiver.type
|
||||
} element, make sure it's a Y type.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function initializeObservers(
|
||||
proto: unknown,
|
||||
receiver: GfxPrimitiveElementModel
|
||||
) {
|
||||
const observers = getObjectPropMeta(proto, observeSymbol);
|
||||
|
||||
Object.keys(observers).forEach(prop => {
|
||||
startObserve(prop, receiver);
|
||||
});
|
||||
|
||||
receiver['_disposable'].add(() => {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
Object.values(receiver[observerDisposableSymbol] ?? {}).forEach(dispose =>
|
||||
(dispose as () => void)()
|
||||
);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import type { GfxPrimitiveElementModel } from '../element-model.js';
|
||||
import { getObjectPropMeta, setObjectPropMeta } from './common.js';
|
||||
|
||||
type WatchFn<T extends GfxPrimitiveElementModel = GfxPrimitiveElementModel> = (
|
||||
oldValue: unknown,
|
||||
instance: T,
|
||||
local: boolean
|
||||
) => void;
|
||||
|
||||
const watchSymbol = Symbol('watch');
|
||||
|
||||
/**
|
||||
* The watch decorator is used to watch the property change of the element.
|
||||
* You can thinks of it as a decorator version of `elementUpdated` slot of the surface model.
|
||||
*/
|
||||
export function watch<V, T extends GfxPrimitiveElementModel>(fn: WatchFn<T>) {
|
||||
return function watchDecorator(
|
||||
_: unknown,
|
||||
context: ClassAccessorDecoratorContext
|
||||
) {
|
||||
const prop = context.name;
|
||||
return {
|
||||
init(this: GfxPrimitiveElementModel, v: V) {
|
||||
setObjectPropMeta(watchSymbol, Object.getPrototypeOf(this), prop, fn);
|
||||
return v;
|
||||
},
|
||||
} as ClassAccessorDecoratorResult<GfxPrimitiveElementModel, V>;
|
||||
};
|
||||
}
|
||||
|
||||
function getWatchMeta(proto: unknown, prop: string | symbol): null | WatchFn {
|
||||
return getObjectPropMeta(proto, watchSymbol, prop);
|
||||
}
|
||||
|
||||
function startWatch(prop: string | symbol, receiver: GfxPrimitiveElementModel) {
|
||||
const proto = Object.getPrototypeOf(receiver);
|
||||
const watchFn = getWatchMeta(proto, prop as string)!;
|
||||
|
||||
if (!watchFn) return;
|
||||
|
||||
receiver['_disposable'].add(
|
||||
receiver.surface.elementUpdated.on(payload => {
|
||||
if (payload.id === receiver.id && prop in payload.props) {
|
||||
watchFn(payload.oldValues[prop as string], receiver, payload.local);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
export function initializeWatchers(
|
||||
prototype: unknown,
|
||||
receiver: GfxPrimitiveElementModel
|
||||
) {
|
||||
const watchers = getObjectPropMeta(prototype, watchSymbol);
|
||||
|
||||
Object.keys(watchers).forEach(prop => {
|
||||
startWatch(prop, receiver);
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,604 @@
|
||||
import {
|
||||
Bound,
|
||||
deserializeXYWH,
|
||||
DisposableGroup,
|
||||
getBoundWithRotation,
|
||||
getPointsFromBoundWithRotation,
|
||||
isEqual,
|
||||
type IVec,
|
||||
linePolygonIntersects,
|
||||
PointLocation,
|
||||
polygonGetPointTangent,
|
||||
polygonNearestPoint,
|
||||
randomSeed,
|
||||
rotatePoints,
|
||||
type SerializedXYWH,
|
||||
Slot,
|
||||
type XYWH,
|
||||
} from '@blocksuite/global/utils';
|
||||
import { DocCollection, type Y } from '@blocksuite/store';
|
||||
import { createMutex } from 'lib0/mutex';
|
||||
|
||||
import {
|
||||
descendantElementsImpl,
|
||||
hasDescendantElementImpl,
|
||||
isLockedByAncestorImpl,
|
||||
isLockedBySelfImpl,
|
||||
isLockedImpl,
|
||||
lockElementImpl,
|
||||
unlockElementImpl,
|
||||
} from '../../../utils/tree.js';
|
||||
import type { EditorHost } from '../../../view/index.js';
|
||||
import type {
|
||||
GfxCompatibleInterface,
|
||||
GfxGroupCompatibleInterface,
|
||||
PointTestOptions,
|
||||
} from '../base.js';
|
||||
import { gfxGroupCompatibleSymbol } from '../base.js';
|
||||
import type { GfxBlockElementModel } from '../gfx-block-model.js';
|
||||
import type { GfxGroupModel, GfxModel } from '../model.js';
|
||||
import {
|
||||
convertProps,
|
||||
field,
|
||||
getDerivedProps,
|
||||
getFieldPropsSet,
|
||||
local,
|
||||
updateDerivedProps,
|
||||
watch,
|
||||
} from './decorators/index.js';
|
||||
import type { SurfaceBlockModel } from './surface-model.js';
|
||||
|
||||
export type BaseElementProps = {
|
||||
index: string;
|
||||
seed: number;
|
||||
lockedBySelf?: boolean;
|
||||
};
|
||||
|
||||
export type SerializedElement = Record<string, unknown> & {
|
||||
type: string;
|
||||
xywh: SerializedXYWH;
|
||||
id: string;
|
||||
index: string;
|
||||
lockedBySelf?: boolean;
|
||||
props: Record<string, unknown>;
|
||||
};
|
||||
export abstract class GfxPrimitiveElementModel<
|
||||
Props extends BaseElementProps = BaseElementProps,
|
||||
> implements GfxCompatibleInterface
|
||||
{
|
||||
private _lastXYWH!: SerializedXYWH;
|
||||
|
||||
protected _disposable = new DisposableGroup();
|
||||
|
||||
protected _id: string;
|
||||
|
||||
protected _local = new Map<string | symbol, unknown>();
|
||||
|
||||
protected _onChange: (payload: {
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
local: boolean;
|
||||
}) => void;
|
||||
|
||||
/**
|
||||
* Used to store a copy of data in the yMap.
|
||||
*/
|
||||
protected _preserved = new Map<string, unknown>();
|
||||
|
||||
protected _stashed: Map<keyof Props | string, unknown>;
|
||||
|
||||
propsUpdated = new Slot<{ key: string }>();
|
||||
|
||||
abstract rotate: number;
|
||||
|
||||
surface!: SurfaceBlockModel;
|
||||
|
||||
abstract xywh: SerializedXYWH;
|
||||
|
||||
yMap: Y.Map<unknown>;
|
||||
|
||||
get connectable() {
|
||||
return true;
|
||||
}
|
||||
|
||||
get deserializedXYWH() {
|
||||
if (!this._lastXYWH || this.xywh !== this._lastXYWH) {
|
||||
const xywh = this.xywh;
|
||||
this._local.set('deserializedXYWH', deserializeXYWH(xywh));
|
||||
this._lastXYWH = xywh;
|
||||
}
|
||||
|
||||
return (this._local.get('deserializedXYWH') as XYWH) ?? [0, 0, 0, 0];
|
||||
}
|
||||
|
||||
/**
|
||||
* The bound of the element after rotation.
|
||||
* The bound without rotation should be created by `Bound.deserialize(this.xywh)`.
|
||||
*/
|
||||
get elementBound() {
|
||||
if (this.rotate) {
|
||||
return Bound.from(getBoundWithRotation(this));
|
||||
}
|
||||
|
||||
return Bound.deserialize(this.xywh);
|
||||
}
|
||||
|
||||
get externalBound(): Bound | null {
|
||||
if (!this._local.has('externalBound')) {
|
||||
const bound = this.externalXYWH
|
||||
? Bound.deserialize(this.externalXYWH)
|
||||
: null;
|
||||
|
||||
this._local.set('externalBound', bound);
|
||||
}
|
||||
|
||||
return this._local.get('externalBound') as Bound | null;
|
||||
}
|
||||
|
||||
get group(): GfxGroupModel | null {
|
||||
return this.surface.getGroup(this.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the ancestor elements in order from the most recent to the earliest.
|
||||
*/
|
||||
get groups(): GfxGroupModel[] {
|
||||
return this.surface.getGroups(this.id);
|
||||
}
|
||||
|
||||
get h() {
|
||||
return this.deserializedXYWH[3];
|
||||
}
|
||||
|
||||
get id() {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
get isConnected() {
|
||||
return this.surface.hasElementById(this.id);
|
||||
}
|
||||
|
||||
get responseBound() {
|
||||
return this.elementBound.expand(this.responseExtension);
|
||||
}
|
||||
|
||||
abstract get type(): string;
|
||||
|
||||
get w() {
|
||||
return this.deserializedXYWH[2];
|
||||
}
|
||||
|
||||
get x() {
|
||||
return this.deserializedXYWH[0];
|
||||
}
|
||||
|
||||
get y() {
|
||||
return this.deserializedXYWH[1];
|
||||
}
|
||||
|
||||
constructor(options: {
|
||||
id: string;
|
||||
yMap: Y.Map<unknown>;
|
||||
model: SurfaceBlockModel;
|
||||
stashedStore: Map<unknown, unknown>;
|
||||
onChange: (payload: {
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
local: boolean;
|
||||
}) => void;
|
||||
}) {
|
||||
const { id, yMap, model, stashedStore, onChange } = options;
|
||||
|
||||
this._id = id;
|
||||
this.yMap = yMap;
|
||||
this.surface = model;
|
||||
this._stashed = stashedStore as Map<keyof Props, unknown>;
|
||||
this._onChange = onChange;
|
||||
|
||||
this.index = 'a0';
|
||||
this.seed = randomSeed();
|
||||
}
|
||||
|
||||
static propsToY(props: Record<string, unknown>) {
|
||||
return props;
|
||||
}
|
||||
|
||||
containsBound(bounds: Bound): boolean {
|
||||
return getPointsFromBoundWithRotation(this).some(point =>
|
||||
bounds.containsPoint(point)
|
||||
);
|
||||
}
|
||||
|
||||
getLineIntersections(start: IVec, end: IVec) {
|
||||
const points = getPointsFromBoundWithRotation(this);
|
||||
return linePolygonIntersects(start, end, points);
|
||||
}
|
||||
|
||||
getNearestPoint(point: IVec) {
|
||||
const points = getPointsFromBoundWithRotation(this);
|
||||
return polygonNearestPoint(points, point);
|
||||
}
|
||||
|
||||
getRelativePointLocation(relativePoint: IVec) {
|
||||
const bound = Bound.deserialize(this.xywh);
|
||||
const point = bound.getRelativePoint(relativePoint);
|
||||
const rotatePoint = rotatePoints([point], bound.center, this.rotate)[0];
|
||||
const points = rotatePoints(bound.points, bound.center, this.rotate);
|
||||
const tangent = polygonGetPointTangent(points, rotatePoint);
|
||||
return new PointLocation(rotatePoint, tangent);
|
||||
}
|
||||
|
||||
includesPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
opt: PointTestOptions,
|
||||
__: EditorHost
|
||||
): boolean {
|
||||
const bound = opt.useElementBound ? this.elementBound : this.responseBound;
|
||||
return bound.isPointInBound([x, y]);
|
||||
}
|
||||
|
||||
intersectsBound(bound: Bound): boolean {
|
||||
return (
|
||||
this.containsBound(bound) ||
|
||||
bound.points.some((point, i, points) =>
|
||||
this.getLineIntersections(point, points[(i + 1) % points.length])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
isLocked(): boolean {
|
||||
return isLockedImpl(this);
|
||||
}
|
||||
|
||||
isLockedByAncestor(): boolean {
|
||||
return isLockedByAncestorImpl(this);
|
||||
}
|
||||
|
||||
isLockedBySelf(): boolean {
|
||||
return isLockedBySelfImpl(this);
|
||||
}
|
||||
|
||||
lock() {
|
||||
lockElementImpl(this.surface.doc, this);
|
||||
}
|
||||
|
||||
onCreated() {}
|
||||
|
||||
onDestroyed() {
|
||||
this._disposable.dispose();
|
||||
this.propsUpdated.dispose();
|
||||
}
|
||||
|
||||
pop(prop: keyof Props | string) {
|
||||
if (!this._stashed.has(prop)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const value = this._stashed.get(prop);
|
||||
this._stashed.delete(prop);
|
||||
// @ts-expect-error FIXME: ts error
|
||||
delete this[prop];
|
||||
|
||||
if (getFieldPropsSet(this).has(prop as string)) {
|
||||
if (!isEqual(value, this.yMap.get(prop as string))) {
|
||||
this.surface.doc.transact(() => {
|
||||
this.yMap.set(prop as string, value);
|
||||
});
|
||||
}
|
||||
} else {
|
||||
console.warn('pop a prop that is not field or local:', prop);
|
||||
}
|
||||
}
|
||||
|
||||
serialize() {
|
||||
const result = this.yMap.toJSON();
|
||||
result.xywh = this.xywh;
|
||||
return result as SerializedElement;
|
||||
}
|
||||
|
||||
stash(prop: keyof Props | string) {
|
||||
if (this._stashed.has(prop)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!getFieldPropsSet(this).has(prop as string)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const curVal = this[prop as unknown as keyof GfxPrimitiveElementModel];
|
||||
|
||||
this._stashed.set(prop, curVal);
|
||||
|
||||
Object.defineProperty(this, prop, {
|
||||
configurable: true,
|
||||
enumerable: true,
|
||||
get: () => this._stashed.get(prop),
|
||||
set: (original: unknown) => {
|
||||
const value = convertProps(prop as string, original, this);
|
||||
const oldValue = this._stashed.get(prop);
|
||||
const derivedProps = getDerivedProps(
|
||||
prop as string,
|
||||
original,
|
||||
this as unknown as GfxPrimitiveElementModel
|
||||
);
|
||||
|
||||
this._stashed.set(prop, value);
|
||||
this._onChange({
|
||||
props: {
|
||||
[prop]: value,
|
||||
},
|
||||
oldValues: {
|
||||
[prop]: oldValue,
|
||||
},
|
||||
local: true,
|
||||
});
|
||||
|
||||
updateDerivedProps(
|
||||
derivedProps,
|
||||
this as unknown as GfxPrimitiveElementModel
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
unlock() {
|
||||
unlockElementImpl(this.surface.doc, this);
|
||||
}
|
||||
|
||||
@local()
|
||||
accessor display: boolean = true;
|
||||
|
||||
/**
|
||||
* In some cases, you need to draw something related to the element, but it does not belong to the element itself.
|
||||
* And it is also interactive, you can select element by clicking on it. E.g. the title of the group element.
|
||||
* In this case, we need to store this kind of external xywh in order to do hit test. This property should not be synced to the doc.
|
||||
* This property should be updated every time it gets rendered.
|
||||
*/
|
||||
@watch((_, instance) => {
|
||||
instance['_local'].delete('externalBound');
|
||||
})
|
||||
@local()
|
||||
accessor externalXYWH: SerializedXYWH | undefined = undefined;
|
||||
|
||||
@field(false)
|
||||
accessor hidden: boolean = false;
|
||||
|
||||
@field()
|
||||
accessor index!: string;
|
||||
|
||||
@field()
|
||||
accessor lockedBySelf: boolean | undefined = false;
|
||||
|
||||
@local()
|
||||
accessor opacity: number = 1;
|
||||
|
||||
@local()
|
||||
accessor responseExtension: [number, number] = [0, 0];
|
||||
|
||||
@field()
|
||||
accessor seed!: number;
|
||||
}
|
||||
|
||||
export abstract class GfxGroupLikeElementModel<
|
||||
Props extends BaseElementProps = BaseElementProps,
|
||||
>
|
||||
extends GfxPrimitiveElementModel<Props>
|
||||
implements GfxGroupCompatibleInterface
|
||||
{
|
||||
private _childIds: string[] = [];
|
||||
|
||||
private _mutex = createMutex();
|
||||
|
||||
abstract children: Y.Map<any>;
|
||||
|
||||
[gfxGroupCompatibleSymbol] = true as const;
|
||||
|
||||
get childElements() {
|
||||
const elements: GfxModel[] = [];
|
||||
|
||||
for (const key of this.childIds) {
|
||||
const element =
|
||||
this.surface.getElementById(key) ||
|
||||
(this.surface.doc.getBlockById(key) as GfxBlockElementModel);
|
||||
|
||||
element && elements.push(element);
|
||||
}
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* The ids of the children. Its role is to provide a unique way to access the children.
|
||||
* You should update this field through `setChildIds` when the children are added or removed.
|
||||
*/
|
||||
get childIds() {
|
||||
return this._childIds;
|
||||
}
|
||||
|
||||
get descendantElements(): GfxModel[] {
|
||||
return descendantElementsImpl(this);
|
||||
}
|
||||
|
||||
get xywh() {
|
||||
this._mutex(() => {
|
||||
const curXYWH =
|
||||
(this._local.get('xywh') as SerializedXYWH) ?? '[0,0,0,0]';
|
||||
const newXYWH = this._getXYWH().serialize();
|
||||
|
||||
if (curXYWH !== newXYWH || !this._local.has('xywh')) {
|
||||
this._local.set('xywh', newXYWH);
|
||||
|
||||
if (curXYWH !== newXYWH) {
|
||||
this._onChange({
|
||||
props: {
|
||||
xywh: newXYWH,
|
||||
},
|
||||
oldValues: {
|
||||
xywh: curXYWH,
|
||||
},
|
||||
local: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return (this._local.get('xywh') as SerializedXYWH) ?? '[0,0,0,0]';
|
||||
}
|
||||
|
||||
set xywh(_) {}
|
||||
|
||||
protected _getXYWH(): Bound {
|
||||
let bound: Bound | undefined;
|
||||
|
||||
this.childElements.forEach(child => {
|
||||
if (child instanceof GfxPrimitiveElementModel && child.hidden) {
|
||||
return;
|
||||
}
|
||||
|
||||
bound = bound ? bound.unite(child.elementBound) : child.elementBound;
|
||||
});
|
||||
|
||||
if (bound) {
|
||||
this._local.set('xywh', bound.serialize());
|
||||
} else {
|
||||
this._local.delete('xywh');
|
||||
}
|
||||
|
||||
return bound ?? new Bound(0, 0, 0, 0);
|
||||
}
|
||||
|
||||
abstract addChild(element: GfxModel): void;
|
||||
|
||||
/**
|
||||
* The actual field that stores the children of the group.
|
||||
* It should be a ymap decorated with `@field`.
|
||||
*/
|
||||
hasChild(element: GfxCompatibleInterface) {
|
||||
return this.childElements.includes(element as GfxModel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the group has the given descendant.
|
||||
*/
|
||||
hasDescendant(element: GfxCompatibleInterface): boolean {
|
||||
return hasDescendantElementImpl(this, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the child from the group
|
||||
*/
|
||||
abstract removeChild(element: GfxCompatibleInterface): void;
|
||||
|
||||
/**
|
||||
* Set the new value of the childIds
|
||||
* @param value the new value of the childIds
|
||||
* @param fromLocal if true, the change is happened in the local
|
||||
*/
|
||||
setChildIds(value: string[], fromLocal: boolean) {
|
||||
const oldChildIds = this.childIds;
|
||||
this._childIds = value;
|
||||
|
||||
this._onChange({
|
||||
props: {
|
||||
childIds: value,
|
||||
},
|
||||
oldValues: {
|
||||
childIds: oldChildIds,
|
||||
},
|
||||
local: fromLocal,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function syncElementFromY(
|
||||
model: GfxPrimitiveElementModel,
|
||||
callback: (payload: {
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
local: boolean;
|
||||
}) => void
|
||||
) {
|
||||
const disposables: Record<string, () => void> = {};
|
||||
const observer = (
|
||||
event: Y.YMapEvent<unknown>,
|
||||
transaction: Y.Transaction
|
||||
) => {
|
||||
const props: Record<string, unknown> = {};
|
||||
const oldValues: Record<string, unknown> = {};
|
||||
|
||||
event.keysChanged.forEach(key => {
|
||||
const type = event.changes.keys.get(key);
|
||||
const oldValue = event.changes.keys.get(key)?.oldValue;
|
||||
|
||||
if (!type) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (type.action === 'update' || type.action === 'add') {
|
||||
const value = model.yMap.get(key);
|
||||
|
||||
if (value instanceof DocCollection.Y.Text) {
|
||||
disposables[key]?.();
|
||||
disposables[key] = watchText(key, value, callback);
|
||||
}
|
||||
|
||||
model['_preserved'].set(key, value);
|
||||
props[key] = value;
|
||||
oldValues[key] = oldValue;
|
||||
} else {
|
||||
model['_preserved'].delete(key);
|
||||
oldValues[key] = oldValue;
|
||||
}
|
||||
});
|
||||
|
||||
callback({
|
||||
props,
|
||||
oldValues,
|
||||
local: transaction.local,
|
||||
});
|
||||
};
|
||||
|
||||
Array.from(model.yMap.entries()).forEach(([key, value]) => {
|
||||
if (value instanceof DocCollection.Y.Text) {
|
||||
disposables[key] = watchText(key, value, callback);
|
||||
}
|
||||
|
||||
model['_preserved'].set(key, value);
|
||||
});
|
||||
|
||||
model.yMap.observe(observer);
|
||||
disposables['ymap'] = () => {
|
||||
model.yMap.unobserve(observer);
|
||||
};
|
||||
|
||||
return () => {
|
||||
Object.values(disposables).forEach(fn => fn());
|
||||
};
|
||||
}
|
||||
|
||||
function watchText(
|
||||
key: string,
|
||||
value: Y.Text,
|
||||
callback: (payload: {
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
local: boolean;
|
||||
}) => void
|
||||
) {
|
||||
const fn = (_: Y.YTextEvent, transaction: Y.Transaction) => {
|
||||
callback({
|
||||
props: {
|
||||
[key]: value,
|
||||
},
|
||||
oldValues: {},
|
||||
local: transaction.local,
|
||||
});
|
||||
};
|
||||
|
||||
value.observe(fn);
|
||||
|
||||
return () => {
|
||||
value.unobserve(fn);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,252 @@
|
||||
import type { IVec, SerializedXYWH, XYWH } from '@blocksuite/global/utils';
|
||||
import {
|
||||
Bound,
|
||||
deserializeXYWH,
|
||||
getPointsFromBoundWithRotation,
|
||||
linePolygonIntersects,
|
||||
PointLocation,
|
||||
polygonGetPointTangent,
|
||||
polygonNearestPoint,
|
||||
rotatePoints,
|
||||
} from '@blocksuite/global/utils';
|
||||
import { mutex } from 'lib0';
|
||||
|
||||
import type { EditorHost } from '../../../view/index.js';
|
||||
import type { GfxCompatibleInterface, PointTestOptions } from '../base.js';
|
||||
import type { GfxGroupModel } from '../model.js';
|
||||
import type { SurfaceBlockModel } from './surface-model.js';
|
||||
|
||||
export function prop<V, T extends GfxLocalElementModel>() {
|
||||
return function propDecorator(
|
||||
_target: ClassAccessorDecoratorTarget<T, V>,
|
||||
context: ClassAccessorDecoratorContext
|
||||
) {
|
||||
const prop = context.name;
|
||||
|
||||
return {
|
||||
init(this: T, val: unknown) {
|
||||
this._props.add(prop);
|
||||
this._local.set(prop, val);
|
||||
},
|
||||
get(this: T) {
|
||||
return this._local.get(prop);
|
||||
},
|
||||
set(this: T, val: V) {
|
||||
this._local.set(prop, val);
|
||||
},
|
||||
} as ClassAccessorDecoratorResult<T, V>;
|
||||
};
|
||||
}
|
||||
|
||||
export abstract class GfxLocalElementModel implements GfxCompatibleInterface {
|
||||
private _mutex: mutex.mutex = mutex.createMutex();
|
||||
|
||||
protected _local = new Map<string | symbol, unknown>();
|
||||
|
||||
/**
|
||||
* Used to store all the name of the properties that have been decorated
|
||||
* with the `@prop`
|
||||
*/
|
||||
protected _props = new Set<string | symbol>();
|
||||
|
||||
protected _surface: SurfaceBlockModel;
|
||||
|
||||
/**
|
||||
* used to store the properties' cache key
|
||||
* when the properties required heavy computation
|
||||
*/
|
||||
cache = new Map<string | symbol, unknown>();
|
||||
|
||||
id: string = '';
|
||||
|
||||
abstract readonly type: string;
|
||||
|
||||
get deserializedXYWH() {
|
||||
if (!this._local.has('deserializedXYWH')) {
|
||||
const xywh = this.xywh;
|
||||
const deserialized = deserializeXYWH(xywh);
|
||||
|
||||
this._local.set('deserializedXYWH', deserialized);
|
||||
}
|
||||
|
||||
return this._local.get('deserializedXYWH') as XYWH;
|
||||
}
|
||||
|
||||
get elementBound() {
|
||||
return new Bound(this.x, this.y, this.w, this.h);
|
||||
}
|
||||
|
||||
get group() {
|
||||
return (
|
||||
this.groupId ? this._surface.getElementById(this.groupId) : null
|
||||
) as GfxGroupModel | null;
|
||||
}
|
||||
|
||||
get groups() {
|
||||
if (this.group) {
|
||||
const groups = this._surface.getGroups(this.group.id);
|
||||
groups.unshift(this.group);
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
get h() {
|
||||
return this.deserializedXYWH[3];
|
||||
}
|
||||
|
||||
get responseBound() {
|
||||
return this.elementBound.expand(this.responseExtension);
|
||||
}
|
||||
|
||||
get surface() {
|
||||
return this._surface;
|
||||
}
|
||||
|
||||
get w() {
|
||||
return this.deserializedXYWH[2];
|
||||
}
|
||||
|
||||
get x() {
|
||||
return this.deserializedXYWH[0];
|
||||
}
|
||||
|
||||
get y() {
|
||||
return this.deserializedXYWH[1];
|
||||
}
|
||||
|
||||
constructor(surfaceModel: SurfaceBlockModel) {
|
||||
this._surface = surfaceModel;
|
||||
|
||||
const p = new Proxy(this, {
|
||||
set: (target, prop, value) => {
|
||||
if (prop === 'xywh') {
|
||||
this._local.delete('deserializedXYWH');
|
||||
}
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
const oldValue = target[prop as string];
|
||||
|
||||
if (oldValue === value) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
target[prop as string] = value;
|
||||
|
||||
if (!this._props.has(prop)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (surfaceModel.localElementModels.has(p)) {
|
||||
this._mutex(() => {
|
||||
surfaceModel.localElementUpdated.emit({
|
||||
model: p,
|
||||
props: {
|
||||
[prop as string]: value,
|
||||
},
|
||||
oldValues: {
|
||||
[prop as string]: oldValue,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
// eslint-disable-next-line no-constructor-return
|
||||
return p;
|
||||
}
|
||||
|
||||
containsBound(bounds: Bound): boolean {
|
||||
return getPointsFromBoundWithRotation(this).some(point =>
|
||||
bounds.containsPoint(point)
|
||||
);
|
||||
}
|
||||
|
||||
getLineIntersections(start: IVec, end: IVec) {
|
||||
const points = getPointsFromBoundWithRotation(this);
|
||||
return linePolygonIntersects(start, end, points);
|
||||
}
|
||||
|
||||
getNearestPoint(point: IVec) {
|
||||
const points = getPointsFromBoundWithRotation(this);
|
||||
return polygonNearestPoint(points, point);
|
||||
}
|
||||
|
||||
getRelativePointLocation(relativePoint: IVec) {
|
||||
const bound = Bound.deserialize(this.xywh);
|
||||
const point = bound.getRelativePoint(relativePoint);
|
||||
const rotatePoint = rotatePoints([point], bound.center, this.rotate)[0];
|
||||
const points = rotatePoints(bound.points, bound.center, this.rotate);
|
||||
const tangent = polygonGetPointTangent(points, rotatePoint);
|
||||
return new PointLocation(rotatePoint, tangent);
|
||||
}
|
||||
|
||||
includesPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
opt: PointTestOptions,
|
||||
__: EditorHost
|
||||
): boolean {
|
||||
const bound = opt.useElementBound ? this.elementBound : this.responseBound;
|
||||
return bound.isPointInBound([x, y]);
|
||||
}
|
||||
|
||||
intersectsBound(bound: Bound): boolean {
|
||||
return (
|
||||
this.containsBound(bound) ||
|
||||
bound.points.some((point, i, points) =>
|
||||
this.getLineIntersections(point, points[(i + 1) % points.length])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
isLocked() {
|
||||
return false;
|
||||
}
|
||||
|
||||
isLockedByAncestor() {
|
||||
return false;
|
||||
}
|
||||
|
||||
isLockedBySelf() {
|
||||
return false;
|
||||
}
|
||||
|
||||
lock() {
|
||||
return;
|
||||
}
|
||||
|
||||
unlock() {
|
||||
return;
|
||||
}
|
||||
|
||||
@prop()
|
||||
accessor groupId: string = '';
|
||||
|
||||
@prop()
|
||||
accessor hidden: boolean = false;
|
||||
|
||||
@prop()
|
||||
accessor index: string = 'a0';
|
||||
|
||||
@prop()
|
||||
accessor opacity: number = 1;
|
||||
|
||||
@prop()
|
||||
accessor responseExtension: [number, number] = [0, 0];
|
||||
|
||||
@prop()
|
||||
accessor rotate: number = 0;
|
||||
|
||||
@prop()
|
||||
accessor seed: number = Math.random();
|
||||
|
||||
@prop()
|
||||
accessor xywh: SerializedXYWH = '[0,0,0,0]';
|
||||
}
|
||||
@@ -0,0 +1,620 @@
|
||||
import { assertType, type Constructor, Slot } from '@blocksuite/global/utils';
|
||||
import type { Boxed, Y } from '@blocksuite/store';
|
||||
import { BlockModel, DocCollection, nanoid } from '@blocksuite/store';
|
||||
|
||||
import {
|
||||
type GfxGroupCompatibleInterface,
|
||||
isGfxGroupCompatibleModel,
|
||||
} from '../base.js';
|
||||
import type { GfxGroupModel, GfxModel } from '../model.js';
|
||||
import { createDecoratorState } from './decorators/common.js';
|
||||
import { initializeObservers, initializeWatchers } from './decorators/index.js';
|
||||
import {
|
||||
GfxGroupLikeElementModel,
|
||||
GfxPrimitiveElementModel,
|
||||
syncElementFromY,
|
||||
} from './element-model.js';
|
||||
import type { GfxLocalElementModel } from './local-element-model.js';
|
||||
|
||||
export type SurfaceBlockProps = {
|
||||
elements: Boxed<Y.Map<Y.Map<unknown>>>;
|
||||
};
|
||||
|
||||
export interface ElementUpdatedData {
|
||||
id: string;
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
local: boolean;
|
||||
}
|
||||
|
||||
export type MiddlewareCtx = {
|
||||
type: 'beforeAdd';
|
||||
payload: {
|
||||
type: string;
|
||||
props: Record<string, unknown>;
|
||||
};
|
||||
};
|
||||
|
||||
export type SurfaceMiddleware = (ctx: MiddlewareCtx) => void;
|
||||
|
||||
export class SurfaceBlockModel extends BlockModel<SurfaceBlockProps> {
|
||||
protected _decoratorState = createDecoratorState();
|
||||
|
||||
protected _elementCtorMap: Record<
|
||||
string,
|
||||
Constructor<
|
||||
GfxPrimitiveElementModel,
|
||||
ConstructorParameters<typeof GfxPrimitiveElementModel>
|
||||
>
|
||||
> = Object.create(null);
|
||||
|
||||
protected _elementModels = new Map<
|
||||
string,
|
||||
{
|
||||
mount: () => void;
|
||||
unmount: () => void;
|
||||
model: GfxPrimitiveElementModel;
|
||||
}
|
||||
>();
|
||||
|
||||
protected _elementTypeMap = new Map<string, GfxPrimitiveElementModel[]>();
|
||||
|
||||
protected _groupLikeModels = new Map<string, GfxGroupModel>();
|
||||
|
||||
protected _middlewares: SurfaceMiddleware[] = [];
|
||||
|
||||
protected _surfaceBlockModel = true;
|
||||
|
||||
elementAdded = new Slot<{ id: string; local: boolean }>();
|
||||
|
||||
elementRemoved = new Slot<{
|
||||
id: string;
|
||||
type: string;
|
||||
model: GfxPrimitiveElementModel;
|
||||
local: boolean;
|
||||
}>();
|
||||
|
||||
elementUpdated = new Slot<ElementUpdatedData>();
|
||||
|
||||
localElementAdded = new Slot<GfxLocalElementModel>();
|
||||
|
||||
localElementDeleted = new Slot<GfxLocalElementModel>();
|
||||
|
||||
protected localElements = new Set<GfxLocalElementModel>();
|
||||
|
||||
localElementUpdated = new Slot<{
|
||||
model: GfxLocalElementModel;
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
}>();
|
||||
|
||||
get elementModels() {
|
||||
const models: GfxPrimitiveElementModel[] = [];
|
||||
this._elementModels.forEach(model => models.push(model.model));
|
||||
return models;
|
||||
}
|
||||
|
||||
get localElementModels() {
|
||||
return this.localElements;
|
||||
}
|
||||
|
||||
get registeredElementTypes() {
|
||||
return Object.keys(this._elementCtorMap);
|
||||
}
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.created.once(() => this._init());
|
||||
}
|
||||
|
||||
private _createElementFromProps(
|
||||
props: Record<string, unknown>,
|
||||
options: {
|
||||
onChange: (payload: {
|
||||
id: string;
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
local: boolean;
|
||||
}) => void;
|
||||
}
|
||||
) {
|
||||
const { type, id, ...rest } = props;
|
||||
|
||||
if (!id) {
|
||||
throw new Error('Cannot find id in props');
|
||||
}
|
||||
|
||||
const yMap = new DocCollection.Y.Map();
|
||||
const elementModel = this._createElementFromYMap(
|
||||
type as string,
|
||||
id as string,
|
||||
yMap,
|
||||
{
|
||||
...options,
|
||||
newCreate: true,
|
||||
}
|
||||
);
|
||||
|
||||
props = this._propsToY(type as string, props);
|
||||
|
||||
yMap.set('type', type);
|
||||
yMap.set('id', id);
|
||||
|
||||
Object.keys(rest).forEach(key => {
|
||||
if (props[key] !== undefined) {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
elementModel.model[key] = props[key];
|
||||
}
|
||||
});
|
||||
|
||||
return elementModel;
|
||||
}
|
||||
|
||||
private _createElementFromYMap(
|
||||
type: string,
|
||||
id: string,
|
||||
yMap: Y.Map<unknown>,
|
||||
options: {
|
||||
onChange: (payload: {
|
||||
id: string;
|
||||
props: Record<string, unknown>;
|
||||
oldValues: Record<string, unknown>;
|
||||
local: boolean;
|
||||
}) => void;
|
||||
skipFieldInit?: boolean;
|
||||
newCreate?: boolean;
|
||||
}
|
||||
) {
|
||||
const stashed = new Map<string | symbol, unknown>();
|
||||
const Ctor = this._elementCtorMap[type];
|
||||
|
||||
if (!Ctor) {
|
||||
throw new Error(`Invalid element type: ${yMap.get('type')}`);
|
||||
}
|
||||
const state = this._decoratorState;
|
||||
|
||||
state.creating = true;
|
||||
state.skipField = options.skipFieldInit ?? false;
|
||||
|
||||
let mounted = false;
|
||||
// @ts-expect-error FIXME: ts error
|
||||
Ctor['_decoratorState'] = state;
|
||||
|
||||
const elementModel = new Ctor({
|
||||
id,
|
||||
yMap,
|
||||
model: this,
|
||||
stashedStore: stashed,
|
||||
onChange: payload => mounted && options.onChange({ id, ...payload }),
|
||||
}) as GfxPrimitiveElementModel;
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
delete Ctor['_decoratorState'];
|
||||
state.creating = false;
|
||||
state.skipField = false;
|
||||
|
||||
const unmount = () => {
|
||||
mounted = false;
|
||||
elementModel.onDestroyed();
|
||||
};
|
||||
|
||||
const mount = () => {
|
||||
initializeObservers(Ctor.prototype, elementModel);
|
||||
initializeWatchers(Ctor.prototype, elementModel);
|
||||
elementModel['_disposable'].add(
|
||||
syncElementFromY(elementModel, payload => {
|
||||
mounted &&
|
||||
options.onChange({
|
||||
id,
|
||||
...payload,
|
||||
});
|
||||
})
|
||||
);
|
||||
mounted = true;
|
||||
elementModel.onCreated();
|
||||
};
|
||||
|
||||
return {
|
||||
model: elementModel,
|
||||
mount,
|
||||
unmount,
|
||||
};
|
||||
}
|
||||
|
||||
private _initElementModels() {
|
||||
const elementsYMap = this.elements.getValue()!;
|
||||
const addToType = (type: string, model: GfxPrimitiveElementModel) => {
|
||||
const sameTypeElements = this._elementTypeMap.get(type) || [];
|
||||
|
||||
if (sameTypeElements.indexOf(model) === -1) {
|
||||
sameTypeElements.push(model);
|
||||
}
|
||||
|
||||
this._elementTypeMap.set(type, sameTypeElements);
|
||||
|
||||
if (isGfxGroupCompatibleModel(model)) {
|
||||
this._groupLikeModels.set(model.id, model);
|
||||
}
|
||||
};
|
||||
const removeFromType = (type: string, model: GfxPrimitiveElementModel) => {
|
||||
const sameTypeElements = this._elementTypeMap.get(type) || [];
|
||||
const index = sameTypeElements.indexOf(model);
|
||||
|
||||
if (index !== -1) {
|
||||
sameTypeElements.splice(index, 1);
|
||||
}
|
||||
|
||||
if (this._groupLikeModels.has(model.id)) {
|
||||
this._groupLikeModels.delete(model.id);
|
||||
}
|
||||
};
|
||||
const onElementsMapChange = (
|
||||
event: Y.YMapEvent<Y.Map<unknown>>,
|
||||
transaction: Y.Transaction
|
||||
) => {
|
||||
const { changes, keysChanged } = event;
|
||||
const addedElements: {
|
||||
mount: () => void;
|
||||
model: GfxPrimitiveElementModel;
|
||||
}[] = [];
|
||||
const deletedElements: {
|
||||
unmount: () => void;
|
||||
model: GfxPrimitiveElementModel;
|
||||
}[] = [];
|
||||
|
||||
keysChanged.forEach(id => {
|
||||
const change = changes.keys.get(id);
|
||||
const element = this.elements.getValue()!.get(id);
|
||||
|
||||
switch (change?.action) {
|
||||
case 'add':
|
||||
if (element) {
|
||||
const hasModel = this._elementModels.has(id);
|
||||
const model = hasModel
|
||||
? this._elementModels.get(id)!
|
||||
: this._createElementFromYMap(
|
||||
element.get('type') as string,
|
||||
element.get('id') as string,
|
||||
element,
|
||||
{
|
||||
onChange: payload => {
|
||||
this.elementUpdated.emit(payload);
|
||||
Object.keys(payload.props).forEach(key => {
|
||||
model.model.propsUpdated.emit({ key });
|
||||
});
|
||||
},
|
||||
skipFieldInit: true,
|
||||
}
|
||||
);
|
||||
|
||||
!hasModel && this._elementModels.set(id, model);
|
||||
addToType(model.model.type, model.model);
|
||||
addedElements.push(model);
|
||||
}
|
||||
break;
|
||||
case 'delete':
|
||||
if (this._elementModels.has(id)) {
|
||||
const { model, unmount } = this._elementModels.get(id)!;
|
||||
removeFromType(model.type, model);
|
||||
this._elementModels.delete(id);
|
||||
deletedElements.push({ model, unmount });
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
addedElements.forEach(({ mount, model }) => {
|
||||
mount();
|
||||
this.elementAdded.emit({ id: model.id, local: transaction.local });
|
||||
});
|
||||
deletedElements.forEach(({ unmount, model }) => {
|
||||
unmount();
|
||||
this.elementRemoved.emit({
|
||||
id: model.id,
|
||||
type: model.type,
|
||||
model,
|
||||
local: transaction.local,
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
elementsYMap.forEach((val, key) => {
|
||||
const model = this._createElementFromYMap(
|
||||
val.get('type') as string,
|
||||
val.get('id') as string,
|
||||
val,
|
||||
{
|
||||
onChange: payload => {
|
||||
this.elementUpdated.emit(payload),
|
||||
Object.keys(payload.props).forEach(key => {
|
||||
model.model.propsUpdated.emit({ key });
|
||||
});
|
||||
},
|
||||
skipFieldInit: true,
|
||||
}
|
||||
);
|
||||
|
||||
this._elementModels.set(key, model);
|
||||
});
|
||||
|
||||
this._elementModels.forEach(({ mount, model }) => {
|
||||
addToType(model.type, model);
|
||||
mount();
|
||||
});
|
||||
|
||||
Object.values(this.doc.blocks.peek()).forEach(block => {
|
||||
if (isGfxGroupCompatibleModel(block.model)) {
|
||||
this._groupLikeModels.set(block.id, block.model);
|
||||
}
|
||||
});
|
||||
|
||||
elementsYMap.observe(onElementsMapChange);
|
||||
|
||||
const disposable = this.doc.slots.blockUpdated.on(payload => {
|
||||
switch (payload.type) {
|
||||
case 'add':
|
||||
if (isGfxGroupCompatibleModel(payload.model)) {
|
||||
this._groupLikeModels.set(payload.id, payload.model);
|
||||
}
|
||||
break;
|
||||
case 'delete':
|
||||
if (isGfxGroupCompatibleModel(payload.model)) {
|
||||
this._groupLikeModels.delete(payload.id);
|
||||
}
|
||||
{
|
||||
const group = this.getGroup(payload.id);
|
||||
if (group) {
|
||||
// eslint-disable-next-line unicorn/prefer-dom-node-remove
|
||||
group.removeChild(payload.model as GfxModel);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
this.deleted.on(() => {
|
||||
elementsYMap.unobserve(onElementsMapChange);
|
||||
disposable.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
private _propsToY(type: string, props: Record<string, unknown>) {
|
||||
const ctor = this._elementCtorMap[type];
|
||||
|
||||
if (!ctor) {
|
||||
throw new Error(`Invalid element type: ${type}`);
|
||||
}
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
return (ctor.propsToY ?? GfxPrimitiveElementModel.propsToY)(props);
|
||||
}
|
||||
|
||||
private _watchGroupRelationChange() {
|
||||
const isGroup = (
|
||||
element: GfxPrimitiveElementModel
|
||||
): element is GfxGroupLikeElementModel =>
|
||||
element instanceof GfxGroupLikeElementModel;
|
||||
|
||||
const disposable = this.elementUpdated.on(({ id, oldValues }) => {
|
||||
const element = this.getElementById(id)!;
|
||||
|
||||
if (
|
||||
isGroup(element) &&
|
||||
oldValues['childIds'] &&
|
||||
element.childIds.length === 0
|
||||
) {
|
||||
this.deleteElement(id);
|
||||
}
|
||||
});
|
||||
this.deleted.on(() => {
|
||||
disposable.dispose();
|
||||
});
|
||||
}
|
||||
|
||||
protected _extendElement(
|
||||
ctorMap: Record<
|
||||
string,
|
||||
Constructor<
|
||||
GfxPrimitiveElementModel,
|
||||
ConstructorParameters<typeof GfxPrimitiveElementModel>
|
||||
>
|
||||
>
|
||||
) {
|
||||
Object.assign(this._elementCtorMap, ctorMap);
|
||||
}
|
||||
|
||||
protected _init() {
|
||||
this._initElementModels();
|
||||
this._watchGroupRelationChange();
|
||||
}
|
||||
|
||||
addElement<T extends object = Record<string, unknown>>(
|
||||
props: Partial<T> & { type: string }
|
||||
) {
|
||||
if (this.doc.readonly) {
|
||||
throw new Error('Cannot add element in readonly mode');
|
||||
}
|
||||
|
||||
const middlewareCtx: MiddlewareCtx = {
|
||||
type: 'beforeAdd',
|
||||
payload: {
|
||||
type: props.type,
|
||||
props,
|
||||
},
|
||||
};
|
||||
|
||||
this._middlewares.forEach(mid => mid(middlewareCtx));
|
||||
|
||||
props = middlewareCtx.payload.props as Partial<T> & { type: string };
|
||||
|
||||
const id = nanoid();
|
||||
|
||||
// @ts-expect-error FIXME: ts error
|
||||
props.id = id;
|
||||
|
||||
const elementModel = this._createElementFromProps(props, {
|
||||
onChange: payload => {
|
||||
this.elementUpdated.emit(payload);
|
||||
Object.keys(payload.props).forEach(key => {
|
||||
elementModel.model.propsUpdated.emit({ key });
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
this._elementModels.set(id, elementModel);
|
||||
|
||||
this.doc.transact(() => {
|
||||
this.elements.getValue()!.set(id, elementModel.model.yMap);
|
||||
});
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
addLocalElement(elem: GfxLocalElementModel) {
|
||||
this.localElements.add(elem);
|
||||
this.localElementAdded.emit(elem);
|
||||
}
|
||||
|
||||
applyMiddlewares(middlewares: SurfaceMiddleware[]) {
|
||||
this._middlewares = middlewares;
|
||||
}
|
||||
|
||||
deleteElement(id: string) {
|
||||
if (this.doc.readonly) {
|
||||
throw new Error('Cannot remove element in readonly mode');
|
||||
}
|
||||
|
||||
if (!this.hasElementById(id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.doc.transact(() => {
|
||||
const element = this.getElementById(id)!;
|
||||
const group = this.getGroup(id);
|
||||
|
||||
if (element instanceof GfxGroupLikeElementModel) {
|
||||
element.childIds.forEach(childId => {
|
||||
if (this.hasElementById(childId)) {
|
||||
this.deleteElement(childId);
|
||||
} else if (this.doc.hasBlock(childId)) {
|
||||
this.doc.deleteBlock(this.doc.getBlock(childId)!.model);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// eslint-disable-next-line unicorn/prefer-dom-node-remove
|
||||
group?.removeChild(element as GfxModel);
|
||||
|
||||
this.elements.getValue()!.delete(id);
|
||||
});
|
||||
}
|
||||
|
||||
deleteLocalElement(elem: GfxLocalElementModel) {
|
||||
if (this.localElements.delete(elem)) {
|
||||
this.localElementDeleted.emit(elem);
|
||||
}
|
||||
}
|
||||
|
||||
override dispose(): void {
|
||||
super.dispose();
|
||||
|
||||
this.elementAdded.dispose();
|
||||
this.elementRemoved.dispose();
|
||||
this.elementUpdated.dispose();
|
||||
|
||||
this._elementModels.forEach(({ unmount }) => unmount());
|
||||
this._elementModels.clear();
|
||||
}
|
||||
|
||||
getElementById(id: string): GfxPrimitiveElementModel | null {
|
||||
return this._elementModels.get(id)?.model ?? null;
|
||||
}
|
||||
|
||||
getElementsByType(type: string): GfxPrimitiveElementModel[] {
|
||||
return this._elementTypeMap.get(type) || [];
|
||||
}
|
||||
|
||||
getGroup(elem: string | GfxModel): GfxGroupModel | null {
|
||||
elem =
|
||||
typeof elem === 'string'
|
||||
? ((this.getElementById(elem) ??
|
||||
this.doc.getBlock(elem)?.model) as GfxModel)
|
||||
: elem;
|
||||
|
||||
if (!elem) return null;
|
||||
|
||||
assertType<GfxModel>(elem);
|
||||
|
||||
for (const group of this._groupLikeModels.values()) {
|
||||
if (group.hasChild(elem)) {
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
getGroups(id: string): GfxGroupModel[] {
|
||||
const groups: GfxGroupModel[] = [];
|
||||
const visited = new Set<GfxGroupModel>();
|
||||
let group = this.getGroup(id);
|
||||
|
||||
while (group) {
|
||||
if (visited.has(group)) {
|
||||
console.warn('Exists a cycle in group relation');
|
||||
break;
|
||||
}
|
||||
visited.add(group);
|
||||
groups.push(group);
|
||||
group = this.getGroup(group.id);
|
||||
}
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
hasElementById(id: string): boolean {
|
||||
return this._elementModels.has(id);
|
||||
}
|
||||
|
||||
isGroup(element: GfxModel): element is GfxModel & GfxGroupCompatibleInterface;
|
||||
isGroup(id: string): boolean;
|
||||
isGroup(element: string | GfxModel): boolean {
|
||||
if (typeof element === 'string') {
|
||||
const el = this.getElementById(element);
|
||||
if (el) return isGfxGroupCompatibleModel(el);
|
||||
|
||||
const blockModel = this.doc.getBlock(element)?.model;
|
||||
if (blockModel) return isGfxGroupCompatibleModel(blockModel);
|
||||
|
||||
return false;
|
||||
} else {
|
||||
return isGfxGroupCompatibleModel(element);
|
||||
}
|
||||
}
|
||||
|
||||
updateElement<T extends object = Record<string, unknown>>(
|
||||
id: string,
|
||||
props: Partial<T>
|
||||
) {
|
||||
if (this.doc.readonly) {
|
||||
throw new Error('Cannot update element in readonly mode');
|
||||
}
|
||||
|
||||
const elementModel = this.getElementById(id);
|
||||
|
||||
if (!elementModel) {
|
||||
throw new Error(`Element ${id} is not found`);
|
||||
}
|
||||
|
||||
this.doc.transact(() => {
|
||||
props = this._propsToY(
|
||||
elementModel.type,
|
||||
props as Record<string, unknown>
|
||||
) as T;
|
||||
Object.entries(props).forEach(([key, value]) => {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
elementModel[key] = value;
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
385
blocksuite/framework/block-std/src/gfx/selection.ts
Normal file
385
blocksuite/framework/block-std/src/gfx/selection.ts
Normal file
@@ -0,0 +1,385 @@
|
||||
import {
|
||||
assertType,
|
||||
DisposableGroup,
|
||||
getCommonBoundWithRotation,
|
||||
groupBy,
|
||||
type IPoint,
|
||||
Slot,
|
||||
} from '@blocksuite/global/utils';
|
||||
|
||||
import type { CursorSelection, SurfaceSelection } from '../selection/index.js';
|
||||
import type { GfxController } from './controller.js';
|
||||
import { GfxExtension, GfxExtensionIdentifier } from './extension.js';
|
||||
import type { GfxModel } from './model/model.js';
|
||||
import { GfxGroupLikeElementModel } from './model/surface/element-model.js';
|
||||
|
||||
export interface SurfaceSelectionState {
|
||||
/**
|
||||
* The selected elements. Could be blocks or canvas elements
|
||||
*/
|
||||
elements: string[];
|
||||
|
||||
/**
|
||||
* Indicate whether the selected element is in editing mode
|
||||
*/
|
||||
editing?: boolean;
|
||||
|
||||
/**
|
||||
* Cannot be operated, only box is displayed
|
||||
*/
|
||||
inoperable?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* GfxSelectionManager is just a wrapper of std selection providing
|
||||
* convenient method and states in gfx
|
||||
*/
|
||||
export class GfxSelectionManager extends GfxExtension {
|
||||
static override key = 'gfxSelection';
|
||||
|
||||
private _activeGroup: GfxGroupLikeElementModel | null = null;
|
||||
|
||||
private _cursorSelection: CursorSelection | null = null;
|
||||
|
||||
private _lastSurfaceSelections: SurfaceSelection[] = [];
|
||||
|
||||
private _remoteCursorSelectionMap = new Map<number, CursorSelection>();
|
||||
|
||||
private _remoteSelectedSet = new Set<string>();
|
||||
|
||||
private _remoteSurfaceSelectionsMap = new Map<number, SurfaceSelection[]>();
|
||||
|
||||
private _selectedSet = new Set<string>();
|
||||
|
||||
private _surfaceSelections: SurfaceSelection[] = [];
|
||||
|
||||
disposable: DisposableGroup = new DisposableGroup();
|
||||
|
||||
readonly slots = {
|
||||
updated: new Slot<SurfaceSelection[]>(),
|
||||
remoteUpdated: new Slot(),
|
||||
|
||||
cursorUpdated: new Slot<CursorSelection>(),
|
||||
remoteCursorUpdated: new Slot(),
|
||||
};
|
||||
|
||||
get activeGroup() {
|
||||
return this._activeGroup;
|
||||
}
|
||||
|
||||
get cursorSelection() {
|
||||
return this._cursorSelection;
|
||||
}
|
||||
|
||||
get editing() {
|
||||
return this.surfaceSelections.some(sel => sel.editing);
|
||||
}
|
||||
|
||||
get empty() {
|
||||
return this.surfaceSelections.every(sel => sel.elements.length === 0);
|
||||
}
|
||||
|
||||
get firstElement() {
|
||||
return this.selectedElements[0];
|
||||
}
|
||||
|
||||
get inoperable() {
|
||||
return this.surfaceSelections.some(sel => sel.inoperable);
|
||||
}
|
||||
|
||||
get lastSurfaceSelections() {
|
||||
return this._lastSurfaceSelections;
|
||||
}
|
||||
|
||||
get remoteCursorSelectionMap() {
|
||||
return this._remoteCursorSelectionMap;
|
||||
}
|
||||
|
||||
get remoteSelectedSet() {
|
||||
return this._remoteSelectedSet;
|
||||
}
|
||||
|
||||
get remoteSurfaceSelectionsMap() {
|
||||
return this._remoteSurfaceSelectionsMap;
|
||||
}
|
||||
|
||||
get selectedBound() {
|
||||
return getCommonBoundWithRotation(this.selectedElements);
|
||||
}
|
||||
|
||||
get selectedElements() {
|
||||
const elements: GfxModel[] = [];
|
||||
|
||||
this.selectedIds.forEach(id => {
|
||||
const el = this.gfx.getElementById(id) as GfxModel;
|
||||
el && elements.push(el);
|
||||
});
|
||||
|
||||
return elements;
|
||||
}
|
||||
|
||||
get selectedIds() {
|
||||
return [...this._selectedSet];
|
||||
}
|
||||
|
||||
get selectedSet() {
|
||||
return this._selectedSet;
|
||||
}
|
||||
|
||||
get stdSelection() {
|
||||
return this.std.selection;
|
||||
}
|
||||
|
||||
get surfaceModel() {
|
||||
return this.gfx.surface;
|
||||
}
|
||||
|
||||
get surfaceSelections() {
|
||||
return this._surfaceSelections;
|
||||
}
|
||||
|
||||
static override extendGfx(gfx: GfxController): void {
|
||||
Object.defineProperty(gfx, 'selection', {
|
||||
get() {
|
||||
return this.std.get(GfxExtensionIdentifier('gfxSelection'));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
clear() {
|
||||
this.stdSelection.clear();
|
||||
|
||||
this.set({
|
||||
elements: [],
|
||||
editing: false,
|
||||
});
|
||||
}
|
||||
|
||||
clearLast() {
|
||||
this._lastSurfaceSelections = [];
|
||||
}
|
||||
|
||||
equals(selection: SurfaceSelection[]) {
|
||||
let count = 0;
|
||||
let editing = false;
|
||||
const exist = selection.every(sel => {
|
||||
const exist = sel.elements.every(id => this._selectedSet.has(id));
|
||||
|
||||
if (exist) {
|
||||
count += sel.elements.length;
|
||||
}
|
||||
|
||||
if (sel.editing) editing = true;
|
||||
|
||||
return exist;
|
||||
});
|
||||
|
||||
return (
|
||||
exist && count === this._selectedSet.size && editing === this.editing
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* check if the element is selected in local
|
||||
* @param element
|
||||
*/
|
||||
has(element: string) {
|
||||
return this._selectedSet.has(element);
|
||||
}
|
||||
|
||||
/**
|
||||
* check if element is selected by remote peers
|
||||
* @param element
|
||||
*/
|
||||
hasRemote(element: string) {
|
||||
return this._remoteSelectedSet.has(element);
|
||||
}
|
||||
|
||||
isEmpty(selections: SurfaceSelection[]) {
|
||||
return selections.every(sel => sel.elements.length === 0);
|
||||
}
|
||||
|
||||
isInSelectedRect(viewX: number, viewY: number) {
|
||||
const selected = this.selectedElements;
|
||||
if (!selected.length) return false;
|
||||
|
||||
const commonBound = getCommonBoundWithRotation(selected);
|
||||
const [modelX, modelY] = this.gfx.viewport.toModelCoord(viewX, viewY);
|
||||
if (commonBound && commonBound.isPointInBound([modelX, modelY])) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
override mounted() {
|
||||
this.disposable.add(
|
||||
this.stdSelection.slots.changed.on(selections => {
|
||||
const { cursor = [], surface = [] } = groupBy(selections, sel => {
|
||||
if (sel.is('surface')) {
|
||||
return 'surface';
|
||||
} else if (sel.is('cursor')) {
|
||||
return 'cursor';
|
||||
}
|
||||
|
||||
return 'none';
|
||||
});
|
||||
|
||||
assertType<CursorSelection[]>(cursor);
|
||||
assertType<SurfaceSelection[]>(surface);
|
||||
|
||||
if (cursor[0] && !this.cursorSelection?.equals(cursor[0])) {
|
||||
this._cursorSelection = cursor[0];
|
||||
this.slots.cursorUpdated.emit(cursor[0]);
|
||||
}
|
||||
|
||||
if ((surface.length === 0 && this.empty) || this.equals(surface)) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._lastSurfaceSelections = this.surfaceSelections;
|
||||
this._surfaceSelections = surface;
|
||||
this._selectedSet = new Set<string>();
|
||||
|
||||
surface.forEach(sel =>
|
||||
sel.elements.forEach(id => {
|
||||
this._selectedSet.add(id);
|
||||
})
|
||||
);
|
||||
|
||||
this.slots.updated.emit(this.surfaceSelections);
|
||||
})
|
||||
);
|
||||
|
||||
this.disposable.add(
|
||||
this.stdSelection.slots.remoteChanged.on(states => {
|
||||
const surfaceMap = new Map<number, SurfaceSelection[]>();
|
||||
const cursorMap = new Map<number, CursorSelection>();
|
||||
const selectedSet = new Set<string>();
|
||||
|
||||
states.forEach((selections, id) => {
|
||||
let hasTextSelection = false;
|
||||
let hasBlockSelection = false;
|
||||
|
||||
selections.forEach(selection => {
|
||||
if (selection.is('text')) {
|
||||
hasTextSelection = true;
|
||||
}
|
||||
|
||||
if (selection.is('block')) {
|
||||
hasBlockSelection = true;
|
||||
}
|
||||
|
||||
if (selection.is('surface')) {
|
||||
const surfaceSelections = surfaceMap.get(id) ?? [];
|
||||
surfaceSelections.push(selection);
|
||||
surfaceMap.set(id, surfaceSelections);
|
||||
|
||||
selection.elements.forEach(id => selectedSet.add(id));
|
||||
}
|
||||
|
||||
if (selection.is('cursor')) {
|
||||
cursorMap.set(id, selection);
|
||||
}
|
||||
});
|
||||
|
||||
if (hasBlockSelection || hasTextSelection) {
|
||||
surfaceMap.delete(id);
|
||||
}
|
||||
|
||||
if (hasTextSelection) {
|
||||
cursorMap.delete(id);
|
||||
}
|
||||
});
|
||||
|
||||
this._remoteCursorSelectionMap = cursorMap;
|
||||
this._remoteSurfaceSelectionsMap = surfaceMap;
|
||||
this._remoteSelectedSet = selectedSet;
|
||||
|
||||
this.slots.remoteUpdated.emit();
|
||||
this.slots.remoteCursorUpdated.emit();
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
set(selection: SurfaceSelectionState | SurfaceSelection[]) {
|
||||
if (Array.isArray(selection)) {
|
||||
this.stdSelection.setGroup(
|
||||
'gfx',
|
||||
this.cursorSelection ? [...selection, this.cursorSelection] : selection
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const { blocks = [], elements = [] } = groupBy(selection.elements, id => {
|
||||
return this.std.doc.getBlockById(id) ? 'blocks' : 'elements';
|
||||
});
|
||||
let instances: (SurfaceSelection | CursorSelection)[] = [];
|
||||
|
||||
if (elements.length > 0 && this.surfaceModel) {
|
||||
instances.push(
|
||||
this.stdSelection.create(
|
||||
'surface',
|
||||
this.surfaceModel.id,
|
||||
elements,
|
||||
selection.editing ?? false,
|
||||
selection.inoperable
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (blocks.length > 0) {
|
||||
instances = instances.concat(
|
||||
blocks.map(blockId =>
|
||||
this.stdSelection.create(
|
||||
'surface',
|
||||
blockId,
|
||||
[blockId],
|
||||
selection.editing ?? false,
|
||||
selection.inoperable
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
this.stdSelection.setGroup(
|
||||
'gfx',
|
||||
this.cursorSelection
|
||||
? instances.concat([this.cursorSelection])
|
||||
: instances
|
||||
);
|
||||
|
||||
if (instances.length > 0) {
|
||||
this.stdSelection.setGroup('note', []);
|
||||
}
|
||||
|
||||
if (
|
||||
selection.elements.length === 1 &&
|
||||
this.firstElement instanceof GfxGroupLikeElementModel
|
||||
) {
|
||||
this._activeGroup = this.firstElement;
|
||||
} else {
|
||||
if (
|
||||
this.selectedElements.some(ele => ele.group !== this._activeGroup) ||
|
||||
this.selectedElements.length === 0
|
||||
) {
|
||||
this._activeGroup = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setCursor(cursor: CursorSelection | IPoint) {
|
||||
const instance = this.stdSelection.create('cursor', cursor.x, cursor.y);
|
||||
|
||||
this.stdSelection.setGroup('gfx', [...this.surfaceSelections, instance]);
|
||||
}
|
||||
|
||||
override unmounted() {
|
||||
this.disposable.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
declare module './controller.js' {
|
||||
interface GfxController {
|
||||
readonly selection: GfxSelectionManager;
|
||||
}
|
||||
}
|
||||
61
blocksuite/framework/block-std/src/gfx/surface-middleware.ts
Normal file
61
blocksuite/framework/block-std/src/gfx/surface-middleware.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import { type Container, createIdentifier } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
import { Extension } from '../extension/extension.js';
|
||||
import { LifeCycleWatcher } from '../extension/lifecycle-watcher.js';
|
||||
import { StdIdentifier } from '../identifier.js';
|
||||
import type { BlockStdScope } from '../scope/block-std-scope.js';
|
||||
import { onSurfaceAdded } from '../utils/gfx.js';
|
||||
import { GfxControllerIdentifier } from './identifiers.js';
|
||||
import type { SurfaceMiddleware } from './model/surface/surface-model.js';
|
||||
|
||||
export abstract class SurfaceMiddlewareBuilder extends Extension {
|
||||
static key: string = '';
|
||||
|
||||
abstract middleware: SurfaceMiddleware;
|
||||
|
||||
get gfx() {
|
||||
return this.std.provider.get(GfxControllerIdentifier);
|
||||
}
|
||||
|
||||
constructor(protected std: BlockStdScope) {
|
||||
super();
|
||||
}
|
||||
|
||||
static override setup(di: Container) {
|
||||
if (!this.key) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'The surface middleware builder should have a static key property.'
|
||||
);
|
||||
}
|
||||
|
||||
di.addImpl(SurfaceMiddlewareBuilderIdentifier(this.key), this, [
|
||||
StdIdentifier,
|
||||
]);
|
||||
}
|
||||
|
||||
mounted(): void {}
|
||||
|
||||
unmounted(): void {}
|
||||
}
|
||||
|
||||
export const SurfaceMiddlewareBuilderIdentifier =
|
||||
createIdentifier<SurfaceMiddlewareBuilder>('SurfaceMiddlewareBuilder');
|
||||
|
||||
export class SurfaceMiddlewareExtension extends LifeCycleWatcher {
|
||||
static override key: string = 'surfaceMiddleware';
|
||||
|
||||
override mounted(): void {
|
||||
const builders = Array.from(
|
||||
this.std.provider.getAll(SurfaceMiddlewareBuilderIdentifier).values()
|
||||
);
|
||||
|
||||
const dispose = onSurfaceAdded(this.std.doc, surface => {
|
||||
if (surface) {
|
||||
surface.applyMiddlewares(builders.map(builder => builder.middleware));
|
||||
queueMicrotask(() => dispose());
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
556
blocksuite/framework/block-std/src/gfx/tool/tool-controller.ts
Normal file
556
blocksuite/framework/block-std/src/gfx/tool/tool-controller.ts
Normal file
@@ -0,0 +1,556 @@
|
||||
import type { ServiceIdentifier } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import {
|
||||
DisposableGroup,
|
||||
type IBound,
|
||||
type IPoint,
|
||||
Slot,
|
||||
} from '@blocksuite/global/utils';
|
||||
import { Signal } from '@preact/signals-core';
|
||||
|
||||
import type { PointerEventState } from '../../event/index.js';
|
||||
import type { GfxController } from '../controller.js';
|
||||
import { GfxExtension, GfxExtensionIdentifier } from '../extension.js';
|
||||
import {
|
||||
type BaseTool,
|
||||
type GfxToolsFullOptionValue,
|
||||
type GfxToolsMap,
|
||||
type GfxToolsOption,
|
||||
ToolIdentifier,
|
||||
} from './tool.js';
|
||||
|
||||
type BuiltInHookEvent<T> = {
|
||||
data: T;
|
||||
preventDefault(): void;
|
||||
};
|
||||
|
||||
type BuiltInEventMap = {
|
||||
beforeToolUpdate: BuiltInHookEvent<{
|
||||
toolName: keyof GfxToolsMap;
|
||||
}>;
|
||||
toolUpdate: BuiltInHookEvent<{ toolName: keyof GfxToolsMap }>;
|
||||
};
|
||||
|
||||
type BuiltInSlotContext = {
|
||||
[K in keyof BuiltInEventMap]: { event: K } & BuiltInEventMap[K];
|
||||
}[SupportedHooks];
|
||||
|
||||
export type SupportedHooks = keyof BuiltInEventMap;
|
||||
|
||||
const supportedEvents = [
|
||||
'dragStart',
|
||||
'dragEnd',
|
||||
'dragMove',
|
||||
'pointerMove',
|
||||
'contextMenu',
|
||||
'pointerDown',
|
||||
'pointerUp',
|
||||
'click',
|
||||
'doubleClick',
|
||||
'tripleClick',
|
||||
'pointerOut',
|
||||
] as const;
|
||||
|
||||
export type SupportedEvents = (typeof supportedEvents)[number];
|
||||
|
||||
export enum MouseButton {
|
||||
FIFTH = 4,
|
||||
FOURTH = 3,
|
||||
MAIN = 0,
|
||||
MIDDLE = 1,
|
||||
SECONDARY = 2,
|
||||
}
|
||||
|
||||
export interface ToolEventTarget {
|
||||
/**
|
||||
* Add a hook before the event is handled by the tool.
|
||||
* Return false to prevent the tool from handling the event.
|
||||
* @param evtName
|
||||
* @param handler
|
||||
*/
|
||||
addHook<K extends SupportedHooks | SupportedEvents>(
|
||||
evtName: K,
|
||||
handler: (
|
||||
evtState: K extends SupportedHooks
|
||||
? BuiltInEventMap[K]
|
||||
: PointerEventState
|
||||
) => void | boolean
|
||||
): void;
|
||||
}
|
||||
|
||||
export const eventTarget = Symbol('eventTarget');
|
||||
|
||||
export class ToolController extends GfxExtension {
|
||||
static override key = 'ToolController';
|
||||
|
||||
private _builtInHookSlot = new Slot<BuiltInSlotContext>();
|
||||
|
||||
private _disposableGroup = new DisposableGroup();
|
||||
|
||||
private _toolOption$ = new Signal<GfxToolsFullOptionValue>(
|
||||
{} as GfxToolsFullOptionValue
|
||||
);
|
||||
|
||||
private _tools = new Map<string, BaseTool>();
|
||||
|
||||
readonly currentToolName$ = new Signal<keyof GfxToolsMap>();
|
||||
|
||||
readonly dragging$ = new Signal<boolean>(false);
|
||||
|
||||
/**
|
||||
* The area that is being dragged.
|
||||
* The coordinates are in browser space.
|
||||
*/
|
||||
readonly draggingViewArea$ = new Signal<
|
||||
IBound & {
|
||||
startX: number;
|
||||
startY: number;
|
||||
endX: number;
|
||||
endY: number;
|
||||
}
|
||||
>({
|
||||
startX: 0,
|
||||
startY: 0,
|
||||
x: 0,
|
||||
y: 0,
|
||||
w: 0,
|
||||
h: 0,
|
||||
endX: 0,
|
||||
endY: 0,
|
||||
});
|
||||
|
||||
/**
|
||||
* The last mouse move position
|
||||
* The coordinates are in browser space
|
||||
*/
|
||||
readonly lastMousePos$ = new Signal<IPoint>({
|
||||
x: 0,
|
||||
y: 0,
|
||||
});
|
||||
|
||||
get currentTool$() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
const self = this;
|
||||
|
||||
return {
|
||||
get value() {
|
||||
return self._tools.get(self.currentToolName$.value);
|
||||
},
|
||||
peek() {
|
||||
return self._tools.get(self.currentToolName$.peek());
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
get currentToolOption$() {
|
||||
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
||||
const self = this;
|
||||
|
||||
return {
|
||||
peek() {
|
||||
const option = self._toolOption$.peek() as unknown as { type: string };
|
||||
|
||||
if (!option.type) {
|
||||
option.type = '';
|
||||
}
|
||||
|
||||
return option as GfxToolsFullOptionValue;
|
||||
},
|
||||
|
||||
get value(): GfxToolsFullOptionValue {
|
||||
const option = self._toolOption$.value as unknown as { type: string };
|
||||
|
||||
if (!option.type) {
|
||||
option.type = '';
|
||||
}
|
||||
|
||||
return option as GfxToolsFullOptionValue;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The area that is being dragged.
|
||||
* The coordinates are in model space.
|
||||
*/
|
||||
get draggingArea$() {
|
||||
const compute = (peek: boolean) => {
|
||||
const area = peek
|
||||
? this.draggingViewArea$.peek()
|
||||
: this.draggingViewArea$.value;
|
||||
const [startX, startY] = this.gfx.viewport.toModelCoord(
|
||||
area.startX,
|
||||
area.startY
|
||||
);
|
||||
const [endX, endY] = this.gfx.viewport.toModelCoord(area.endX, area.endY);
|
||||
|
||||
return {
|
||||
x: Math.min(startX, endX),
|
||||
y: Math.min(startY, endY),
|
||||
w: Math.abs(endX - startX),
|
||||
h: Math.abs(endY - startY),
|
||||
startX,
|
||||
startY,
|
||||
endX,
|
||||
endY,
|
||||
};
|
||||
};
|
||||
|
||||
return {
|
||||
value() {
|
||||
return compute(false);
|
||||
},
|
||||
peek() {
|
||||
return compute(true);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
static override extendGfx(gfx: GfxController) {
|
||||
Object.defineProperty(gfx, 'tool', {
|
||||
get() {
|
||||
return this.std.provider.get(ToolControllerIdentifier);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private _createBuiltInHookCtx<K extends keyof BuiltInEventMap>(
|
||||
eventName: K,
|
||||
data: BuiltInEventMap[K]['data']
|
||||
): {
|
||||
prevented: boolean;
|
||||
slotCtx: BuiltInSlotContext;
|
||||
} {
|
||||
const ctx = {
|
||||
prevented: false,
|
||||
slotCtx: {
|
||||
event: eventName,
|
||||
data,
|
||||
preventDefault() {
|
||||
ctx.prevented = true;
|
||||
},
|
||||
} as BuiltInSlotContext,
|
||||
};
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
private _initializeEvents() {
|
||||
const hooks: Record<
|
||||
string,
|
||||
((
|
||||
evtState: PointerEventState | BuiltInSlotContext
|
||||
) => undefined | boolean)[]
|
||||
> = {};
|
||||
/**
|
||||
* Invoke the hook and the tool handler.
|
||||
* @returns false if the handler is prevented by the hook
|
||||
*/
|
||||
const invokeToolHandler = (
|
||||
evtName: SupportedEvents,
|
||||
evt: PointerEventState,
|
||||
tool?: BaseTool
|
||||
) => {
|
||||
const evtHooks = hooks[evtName];
|
||||
const stopHandler = evtHooks?.reduce((pre, hook) => {
|
||||
return pre || hook(evt) === false;
|
||||
}, false);
|
||||
|
||||
tool = tool ?? this.currentTool$.peek();
|
||||
|
||||
if (stopHandler) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
tool?.[evtName](evt);
|
||||
return true;
|
||||
} catch (e) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ExecutionError,
|
||||
`Error occurred while executing ${evtName} handler of tool "${tool?.toolName}"`,
|
||||
{
|
||||
cause: e as Error,
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Hook into the event lifecycle.
|
||||
* All hooks will be executed despite the current active tool.
|
||||
* This is useful for tools that need to perform some action before an event is handled.
|
||||
* @param evtName
|
||||
* @param handler
|
||||
*/
|
||||
const addHook: ToolEventTarget['addHook'] = (evtName, handler) => {
|
||||
hooks[evtName] = hooks[evtName] ?? [];
|
||||
hooks[evtName].push(
|
||||
handler as (
|
||||
evtState: PointerEventState | BuiltInSlotContext
|
||||
) => undefined | boolean
|
||||
);
|
||||
|
||||
return () => {
|
||||
const idx = hooks[evtName].indexOf(
|
||||
handler as (
|
||||
evtState: PointerEventState | BuiltInSlotContext
|
||||
) => undefined | boolean
|
||||
);
|
||||
if (idx !== -1) {
|
||||
hooks[evtName].splice(idx, 1);
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
let dragContext: {
|
||||
tool: BaseTool;
|
||||
} | null = null;
|
||||
|
||||
this._disposableGroup.add(
|
||||
this.std.event.add('dragStart', ctx => {
|
||||
const evt = ctx.get('pointerState');
|
||||
|
||||
if (
|
||||
evt.button === MouseButton.SECONDARY &&
|
||||
!this.currentTool$.peek()?.allowDragWithRightButton
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (evt.button === MouseButton.MIDDLE) {
|
||||
evt.raw.preventDefault();
|
||||
}
|
||||
|
||||
this.dragging$.value = true;
|
||||
this.draggingViewArea$.value = {
|
||||
startX: evt.x,
|
||||
startY: evt.y,
|
||||
endX: evt.x,
|
||||
endY: evt.y,
|
||||
x: evt.x,
|
||||
y: evt.y,
|
||||
w: 0,
|
||||
h: 0,
|
||||
};
|
||||
|
||||
// this means the dragEnd event is not even fired
|
||||
// so we need to manually call the dragEnd method
|
||||
if (dragContext?.tool) {
|
||||
dragContext.tool.dragEnd(evt);
|
||||
dragContext = null;
|
||||
}
|
||||
|
||||
if (invokeToolHandler('dragStart', evt)) {
|
||||
dragContext = this.currentTool$.peek()
|
||||
? {
|
||||
tool: this.currentTool$.peek()!,
|
||||
}
|
||||
: null;
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
this._disposableGroup.add(
|
||||
this.std.event.add('dragMove', ctx => {
|
||||
if (!this.dragging$.peek()) {
|
||||
return;
|
||||
}
|
||||
|
||||
const evt = ctx.get('pointerState');
|
||||
const draggingStart = {
|
||||
x: this.draggingArea$.peek().startX,
|
||||
y: this.draggingArea$.peek().startY,
|
||||
originX: this.draggingViewArea$.peek().startX,
|
||||
originY: this.draggingViewArea$.peek().startY,
|
||||
};
|
||||
|
||||
this.draggingViewArea$.value = {
|
||||
...this.draggingViewArea$.peek(),
|
||||
w: Math.abs(evt.x - draggingStart.originX),
|
||||
h: Math.abs(evt.y - draggingStart.originY),
|
||||
x: Math.min(evt.x, draggingStart.originX),
|
||||
y: Math.min(evt.y, draggingStart.originY),
|
||||
endX: evt.x,
|
||||
endY: evt.y,
|
||||
};
|
||||
|
||||
invokeToolHandler('dragMove', evt, dragContext?.tool);
|
||||
})
|
||||
);
|
||||
|
||||
this._disposableGroup.add(
|
||||
this.std.event.add('dragEnd', ctx => {
|
||||
if (!this.dragging$.peek()) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.dragging$.value = false;
|
||||
const evt = ctx.get('pointerState');
|
||||
|
||||
// if the tool dragEnd is prevented by the hook, call the dragEnd method manually
|
||||
// this guarantee the dragStart and dragEnd events are always called together
|
||||
if (
|
||||
!invokeToolHandler('dragEnd', evt, dragContext?.tool) &&
|
||||
dragContext?.tool
|
||||
) {
|
||||
dragContext.tool.dragEnd(evt);
|
||||
}
|
||||
|
||||
dragContext = null;
|
||||
this.draggingViewArea$.value = {
|
||||
x: 0,
|
||||
y: 0,
|
||||
startX: 0,
|
||||
startY: 0,
|
||||
endX: 0,
|
||||
endY: 0,
|
||||
w: 0,
|
||||
h: 0,
|
||||
};
|
||||
})
|
||||
);
|
||||
|
||||
this._disposableGroup.add(
|
||||
this.std.event.add('pointerMove', ctx => {
|
||||
const evt = ctx.get('pointerState');
|
||||
|
||||
this.lastMousePos$.value = {
|
||||
x: evt.x,
|
||||
y: evt.y,
|
||||
};
|
||||
|
||||
invokeToolHandler('pointerMove', evt);
|
||||
})
|
||||
);
|
||||
|
||||
this._disposableGroup.add(
|
||||
this.std.event.add('contextMenu', ctx => {
|
||||
const evt = ctx.get('defaultState');
|
||||
|
||||
// when in editing mode, allow context menu to pop up
|
||||
if (this.gfx.selection.editing) return;
|
||||
|
||||
evt.event.preventDefault();
|
||||
})
|
||||
);
|
||||
|
||||
supportedEvents.slice(5).forEach(evtName => {
|
||||
this._disposableGroup.add(
|
||||
this.std.event.add(evtName, ctx => {
|
||||
const evt = ctx.get('pointerState');
|
||||
|
||||
invokeToolHandler(evtName, evt);
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
this._builtInHookSlot.on(evt => {
|
||||
hooks[evt.event]?.forEach(hook => hook(evt));
|
||||
});
|
||||
|
||||
return {
|
||||
addHook,
|
||||
};
|
||||
}
|
||||
|
||||
private _register(tools: BaseTool) {
|
||||
if (this._tools.has(tools.toolName)) {
|
||||
this._tools.get(tools.toolName)?.unmounted();
|
||||
}
|
||||
|
||||
this._tools.set(tools.toolName, tools);
|
||||
tools.mounted();
|
||||
}
|
||||
|
||||
get<K extends keyof GfxToolsMap>(key: K): GfxToolsMap[K] {
|
||||
return this._tools.get(key) as GfxToolsMap[K];
|
||||
}
|
||||
|
||||
override mounted(): void {
|
||||
const { addHook } = this._initializeEvents();
|
||||
|
||||
const eventTarget: ToolEventTarget = {
|
||||
addHook,
|
||||
};
|
||||
|
||||
this.std.provider.getAll(ToolIdentifier).forEach(tool => {
|
||||
// @ts-expect-error FIXME: ts error
|
||||
tool['eventTarget'] = eventTarget;
|
||||
this._register(tool);
|
||||
});
|
||||
}
|
||||
|
||||
setTool(toolName: GfxToolsFullOptionValue, ...args: [void]): void;
|
||||
setTool<K extends keyof GfxToolsMap>(
|
||||
toolName: K,
|
||||
...args: K extends keyof GfxToolsOption
|
||||
? [option: GfxToolsOption[K]]
|
||||
: [void]
|
||||
): void;
|
||||
setTool<K extends keyof GfxToolsMap>(
|
||||
toolName: K | GfxToolsFullOptionValue,
|
||||
...args: K extends keyof GfxToolsOption
|
||||
? [option: GfxToolsOption[K]]
|
||||
: [void]
|
||||
): void {
|
||||
const option = typeof toolName === 'string' ? args[0] : toolName;
|
||||
const toolNameStr =
|
||||
typeof toolName === 'string'
|
||||
? toolName
|
||||
: ((toolName as { type: string }).type as K);
|
||||
|
||||
const beforeUpdateCtx = this._createBuiltInHookCtx('beforeToolUpdate', {
|
||||
toolName: toolNameStr,
|
||||
});
|
||||
this._builtInHookSlot.emit(beforeUpdateCtx.slotCtx);
|
||||
|
||||
if (beforeUpdateCtx.prevented) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.gfx.selection.set({ elements: [] });
|
||||
|
||||
this.currentTool$.peek()?.deactivate();
|
||||
this.currentToolName$.value = toolNameStr;
|
||||
|
||||
const currentTool = this.currentTool$.peek();
|
||||
if (!currentTool) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
`Tool "${this.currentToolName$.value}" is not defined`
|
||||
);
|
||||
}
|
||||
|
||||
currentTool.activatedOption = option ?? {};
|
||||
this._toolOption$.value = {
|
||||
...currentTool.activatedOption,
|
||||
type: toolNameStr,
|
||||
} as GfxToolsFullOptionValue;
|
||||
currentTool.activate(currentTool.activatedOption);
|
||||
|
||||
const afterUpdateCtx = this._createBuiltInHookCtx('toolUpdate', {
|
||||
toolName: toolNameStr,
|
||||
});
|
||||
this._builtInHookSlot.emit(afterUpdateCtx.slotCtx);
|
||||
}
|
||||
|
||||
override unmounted(): void {
|
||||
this.currentTool$.peek()?.deactivate();
|
||||
this._tools.forEach(tool => {
|
||||
tool.unmounted();
|
||||
tool['disposable'].dispose();
|
||||
});
|
||||
this._builtInHookSlot.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
export const ToolControllerIdentifier = GfxExtensionIdentifier(
|
||||
'ToolController'
|
||||
) as ServiceIdentifier<ToolController>;
|
||||
|
||||
declare module '../controller.js' {
|
||||
interface GfxController {
|
||||
readonly tool: ToolController;
|
||||
}
|
||||
}
|
||||
125
blocksuite/framework/block-std/src/gfx/tool/tool.ts
Normal file
125
blocksuite/framework/block-std/src/gfx/tool/tool.ts
Normal file
@@ -0,0 +1,125 @@
|
||||
import { type Container, createIdentifier } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { DisposableGroup } from '@blocksuite/global/utils';
|
||||
|
||||
import type { PointerEventState } from '../../event/index.js';
|
||||
import { Extension } from '../../extension/extension.js';
|
||||
import type { GfxController } from '../controller.js';
|
||||
import { GfxControllerIdentifier } from '../identifiers.js';
|
||||
import type { ToolEventTarget } from './tool-controller.js';
|
||||
|
||||
export abstract class BaseTool<
|
||||
Option = Record<string, unknown>,
|
||||
> extends Extension {
|
||||
static toolName: string = '';
|
||||
|
||||
private readonly eventTarget!: ToolEventTarget;
|
||||
|
||||
activatedOption: Option = {} as Option;
|
||||
|
||||
addHook: ToolEventTarget['addHook'] = (evtName, handler) => {
|
||||
this.eventTarget.addHook(evtName, handler);
|
||||
};
|
||||
|
||||
/**
|
||||
* The `disposable` will be disposed when the tool is unloaded.
|
||||
*/
|
||||
protected readonly disposable = new DisposableGroup();
|
||||
|
||||
get active() {
|
||||
return this.gfx.tool.currentTool$.peek() === this;
|
||||
}
|
||||
|
||||
get allowDragWithRightButton() {
|
||||
return false;
|
||||
}
|
||||
|
||||
get controller() {
|
||||
return this.gfx.tool;
|
||||
}
|
||||
|
||||
get doc() {
|
||||
return this.gfx.doc;
|
||||
}
|
||||
|
||||
get std() {
|
||||
return this.gfx.std;
|
||||
}
|
||||
|
||||
get toolName() {
|
||||
return (this.constructor as typeof BaseTool).toolName;
|
||||
}
|
||||
|
||||
constructor(readonly gfx: GfxController) {
|
||||
super();
|
||||
}
|
||||
|
||||
static override setup(di: Container): void {
|
||||
if (!this.toolName) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
`The tool constructor '${this.name}' should have a static 'toolName' property.`
|
||||
);
|
||||
}
|
||||
|
||||
di.addImpl(ToolIdentifier(this.toolName), this, [GfxControllerIdentifier]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when the tool is activated.
|
||||
* @param _ - The data passed as second argument when calling `ToolController.use`.
|
||||
*/
|
||||
activate(_: Option): void {}
|
||||
|
||||
click(_: PointerEventState): void {}
|
||||
|
||||
contextMenu(_: PointerEventState): void {}
|
||||
|
||||
/**
|
||||
* Called when the tool is deactivated.
|
||||
*/
|
||||
deactivate(): void {}
|
||||
|
||||
doubleClick(_: PointerEventState): void {}
|
||||
|
||||
dragEnd(_: PointerEventState): void {}
|
||||
|
||||
dragMove(_: PointerEventState): void {}
|
||||
|
||||
dragStart(_: PointerEventState): void {}
|
||||
|
||||
/**
|
||||
* Called when the tool is registered.
|
||||
*/
|
||||
mounted(): void {}
|
||||
|
||||
pointerDown(_: PointerEventState): void {}
|
||||
|
||||
pointerMove(_: PointerEventState): void {}
|
||||
|
||||
pointerOut(_: PointerEventState): void {}
|
||||
|
||||
pointerUp(_: PointerEventState): void {}
|
||||
|
||||
tripleClick(_: PointerEventState): void {}
|
||||
|
||||
/**
|
||||
* Called when the tool is unloaded, usually when the whole `ToolController` is destroyed.
|
||||
*/
|
||||
unmounted(): void {}
|
||||
}
|
||||
|
||||
export const ToolIdentifier = createIdentifier<BaseTool>('GfxTool');
|
||||
|
||||
export interface GfxToolsMap {}
|
||||
|
||||
export interface GfxToolsOption {}
|
||||
|
||||
export type GfxToolsFullOption = {
|
||||
[Key in keyof GfxToolsMap]: Key extends keyof GfxToolsOption
|
||||
? { type: Key } & GfxToolsOption[Key]
|
||||
: { type: Key };
|
||||
};
|
||||
|
||||
export type GfxToolsFullOptionValue =
|
||||
GfxToolsFullOption[keyof GfxToolsFullOption];
|
||||
130
blocksuite/framework/block-std/src/gfx/view/view-manager.ts
Normal file
130
blocksuite/framework/block-std/src/gfx/view/view-manager.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { DisposableGroup } from '@blocksuite/global/utils';
|
||||
|
||||
import { onSurfaceAdded } from '../../utils/gfx.js';
|
||||
import type { GfxController } from '../controller.js';
|
||||
import { GfxExtension, GfxExtensionIdentifier } from '../extension.js';
|
||||
import { GfxBlockElementModel } from '../model/gfx-block-model.js';
|
||||
import type { GfxModel } from '../model/model.js';
|
||||
import type { GfxLocalElementModel } from '../model/surface/local-element-model.js';
|
||||
import type { SurfaceBlockModel } from '../model/surface/surface-model.js';
|
||||
import {
|
||||
GfxElementModelView,
|
||||
GfxElementModelViewExtIdentifier,
|
||||
} from './view.js';
|
||||
|
||||
export class ViewManager extends GfxExtension {
|
||||
static override key = 'viewManager';
|
||||
|
||||
private _disposable = new DisposableGroup();
|
||||
|
||||
private _viewCtorMap = new Map<string, typeof GfxElementModelView>();
|
||||
|
||||
private _viewMap = new Map<string, GfxElementModelView>();
|
||||
|
||||
constructor(gfx: GfxController) {
|
||||
super(gfx);
|
||||
}
|
||||
|
||||
static override extendGfx(gfx: GfxController): void {
|
||||
Object.defineProperty(gfx, 'view', {
|
||||
get() {
|
||||
return this.std.get(GfxExtensionIdentifier('viewManager'));
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
get(model: GfxModel | GfxLocalElementModel | string) {
|
||||
if (typeof model === 'string') {
|
||||
if (this._viewMap.has(model)) {
|
||||
return this._viewMap.get(model);
|
||||
}
|
||||
|
||||
return this.std.view.getBlock(model) ?? null;
|
||||
} else {
|
||||
if (model instanceof GfxBlockElementModel) {
|
||||
return this.std.view.getBlock(model.id) ?? null;
|
||||
} else {
|
||||
return this._viewMap.get(model.id) ?? null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override mounted(): void {
|
||||
this.std.provider
|
||||
.getAll(GfxElementModelViewExtIdentifier)
|
||||
.forEach(viewCtor => {
|
||||
this._viewCtorMap.set(viewCtor.type, viewCtor);
|
||||
});
|
||||
|
||||
const updateViewOnElementChange = (surface: SurfaceBlockModel) => {
|
||||
this._disposable.add(
|
||||
surface.elementAdded.on(payload => {
|
||||
const model = surface.getElementById(payload.id)!;
|
||||
const View = this._viewCtorMap.get(model.type) ?? GfxElementModelView;
|
||||
|
||||
this._viewMap.set(model.id, new View(model, this.gfx));
|
||||
})
|
||||
);
|
||||
|
||||
this._disposable.add(
|
||||
surface.elementRemoved.on(elem => {
|
||||
const view = this._viewMap.get(elem.id);
|
||||
this._viewMap.delete(elem.id);
|
||||
view?.onDestroyed();
|
||||
})
|
||||
);
|
||||
|
||||
this._disposable.add(
|
||||
surface.localElementAdded.on(model => {
|
||||
const View = this._viewCtorMap.get(model.type) ?? GfxElementModelView;
|
||||
|
||||
this._viewMap.set(model.id, new View(model, this.gfx));
|
||||
})
|
||||
);
|
||||
|
||||
this._disposable.add(
|
||||
surface.localElementDeleted.on(model => {
|
||||
const view = this._viewMap.get(model.id);
|
||||
this._viewMap.delete(model.id);
|
||||
view?.onDestroyed();
|
||||
})
|
||||
);
|
||||
|
||||
surface.localElementModels.forEach(model => {
|
||||
const View = this._viewCtorMap.get(model.type) ?? GfxElementModelView;
|
||||
|
||||
this._viewMap.set(model.id, new View(model, this.gfx));
|
||||
});
|
||||
|
||||
surface.elementModels.forEach(model => {
|
||||
const View = this._viewCtorMap.get(model.type) ?? GfxElementModelView;
|
||||
|
||||
this._viewMap.set(model.id, new View(model, this.gfx));
|
||||
});
|
||||
};
|
||||
|
||||
if (this.gfx.surface) {
|
||||
updateViewOnElementChange(this.gfx.surface);
|
||||
} else {
|
||||
this._disposable.add(
|
||||
onSurfaceAdded(this.std.doc, surface => {
|
||||
if (surface) {
|
||||
updateViewOnElementChange(surface);
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
override unmounted(): void {
|
||||
this._disposable.dispose();
|
||||
this._viewMap.forEach(view => view.onDestroyed());
|
||||
this._viewMap.clear();
|
||||
}
|
||||
}
|
||||
|
||||
declare module '../controller.js' {
|
||||
interface GfxController {
|
||||
readonly view: ViewManager;
|
||||
}
|
||||
}
|
||||
181
blocksuite/framework/block-std/src/gfx/view/view.ts
Normal file
181
blocksuite/framework/block-std/src/gfx/view/view.ts
Normal file
@@ -0,0 +1,181 @@
|
||||
import { type Container, createIdentifier } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import {
|
||||
type Bound,
|
||||
DisposableGroup,
|
||||
type IVec,
|
||||
} from '@blocksuite/global/utils';
|
||||
|
||||
import type { PointerEventState } from '../../event/index.js';
|
||||
import type { Extension } from '../../extension/extension.js';
|
||||
import type { EditorHost } from '../../view/index.js';
|
||||
import type { GfxController } from '../index.js';
|
||||
import type { GfxElementGeometry, PointTestOptions } from '../model/base.js';
|
||||
import type { GfxPrimitiveElementModel } from '../model/surface/element-model.js';
|
||||
import type { GfxLocalElementModel } from '../model/surface/local-element-model.js';
|
||||
|
||||
export type EventsHandlerMap = {
|
||||
click: PointerEventState;
|
||||
dblclick: PointerEventState;
|
||||
pointerdown: PointerEventState;
|
||||
pointerenter: PointerEventState;
|
||||
pointerleave: PointerEventState;
|
||||
pointermove: PointerEventState;
|
||||
pointerup: PointerEventState;
|
||||
};
|
||||
|
||||
export type SupportedEvent = keyof EventsHandlerMap;
|
||||
|
||||
export const GfxElementModelViewExtIdentifier = createIdentifier<
|
||||
typeof GfxElementModelView
|
||||
>('GfxElementModelView');
|
||||
|
||||
export class GfxElementModelView<
|
||||
T extends GfxLocalElementModel | GfxPrimitiveElementModel =
|
||||
| GfxPrimitiveElementModel
|
||||
| GfxLocalElementModel,
|
||||
RendererContext = object,
|
||||
>
|
||||
implements GfxElementGeometry, Extension
|
||||
{
|
||||
static type: string;
|
||||
|
||||
private _handlers = new Map<keyof EventsHandlerMap, ((evt: any) => void)[]>();
|
||||
|
||||
private _isConnected = true;
|
||||
|
||||
protected disposable = new DisposableGroup();
|
||||
|
||||
readonly model: T;
|
||||
|
||||
get isConnected() {
|
||||
return this._isConnected;
|
||||
}
|
||||
|
||||
get rotate() {
|
||||
return this.model.rotate;
|
||||
}
|
||||
|
||||
get surface() {
|
||||
return this.model.surface;
|
||||
}
|
||||
|
||||
get type() {
|
||||
return this.model.type;
|
||||
}
|
||||
|
||||
constructor(
|
||||
model: T,
|
||||
readonly gfx: GfxController
|
||||
) {
|
||||
this.model = model;
|
||||
this.onCreated();
|
||||
}
|
||||
|
||||
static setup(di: Container): void {
|
||||
if (!this.type) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'The GfxElementModelView should have a static `type` property.'
|
||||
);
|
||||
}
|
||||
|
||||
di.addImpl(GfxElementModelViewExtIdentifier(this.type), () => this);
|
||||
}
|
||||
|
||||
containsBound(bounds: Bound): boolean {
|
||||
return this.model.containsBound(bounds);
|
||||
}
|
||||
|
||||
dispatch<K extends keyof EventsHandlerMap>(
|
||||
event: K,
|
||||
evt: EventsHandlerMap[K]
|
||||
) {
|
||||
this._handlers.get(event)?.forEach(callback => callback(evt));
|
||||
}
|
||||
|
||||
getLineIntersections(start: IVec, end: IVec) {
|
||||
return this.model.getLineIntersections(start, end);
|
||||
}
|
||||
|
||||
getNearestPoint(point: IVec) {
|
||||
return this.model.getNearestPoint(point);
|
||||
}
|
||||
|
||||
getRelativePointLocation(relativePoint: IVec) {
|
||||
return this.model.getRelativePointLocation(relativePoint);
|
||||
}
|
||||
|
||||
includesPoint(
|
||||
x: number,
|
||||
y: number,
|
||||
_: PointTestOptions,
|
||||
__: EditorHost
|
||||
): boolean {
|
||||
return this.model.includesPoint(x, y, _, __);
|
||||
}
|
||||
|
||||
intersectsBound(bound: Bound): boolean {
|
||||
return (
|
||||
this.containsBound(bound) ||
|
||||
bound.points.some((point, i, points) =>
|
||||
this.getLineIntersections(point, points[(i + 1) % points.length])
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
off<K extends keyof EventsHandlerMap>(
|
||||
event: K,
|
||||
callback: (evt: EventsHandlerMap[K]) => void
|
||||
) {
|
||||
if (!this._handlers.has(event)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const callbacks = this._handlers.get(event)!;
|
||||
const index = callbacks.indexOf(callback);
|
||||
|
||||
if (index !== -1) {
|
||||
callbacks.splice(index, 1);
|
||||
}
|
||||
}
|
||||
|
||||
on<K extends keyof EventsHandlerMap>(
|
||||
event: K,
|
||||
callback: (evt: EventsHandlerMap[K]) => void
|
||||
) {
|
||||
if (!this._handlers.has(event)) {
|
||||
this._handlers.set(event, []);
|
||||
}
|
||||
|
||||
this._handlers.get(event)!.push(callback);
|
||||
|
||||
return () => this.off(event, callback);
|
||||
}
|
||||
|
||||
once<K extends keyof EventsHandlerMap>(
|
||||
event: K,
|
||||
callback: (evt: EventsHandlerMap[K]) => void
|
||||
) {
|
||||
const off = this.on(event, evt => {
|
||||
off();
|
||||
callback(evt);
|
||||
});
|
||||
|
||||
return off;
|
||||
}
|
||||
|
||||
onCreated() {}
|
||||
|
||||
/**
|
||||
* Called when the view is destroyed.
|
||||
* Override this method requires calling `super.onDestroyed()`.
|
||||
*/
|
||||
onDestroyed() {
|
||||
this._isConnected = false;
|
||||
this.disposable.dispose();
|
||||
this._handlers.clear();
|
||||
}
|
||||
|
||||
render(_: RendererContext) {}
|
||||
}
|
||||
159
blocksuite/framework/block-std/src/gfx/viewport-element.ts
Normal file
159
blocksuite/framework/block-std/src/gfx/viewport-element.ts
Normal file
@@ -0,0 +1,159 @@
|
||||
import { WithDisposable } from '@blocksuite/global/utils';
|
||||
import { css, html } from 'lit';
|
||||
import { property } from 'lit/decorators.js';
|
||||
|
||||
import { PropTypes, requiredProperties } from '../view/decorators/required.js';
|
||||
import { type EditorHost, ShadowlessElement } from '../view/index.js';
|
||||
import type { GfxBlockElementModel } from './model/gfx-block-model.js';
|
||||
import { Viewport } from './viewport.js';
|
||||
|
||||
/**
|
||||
* A wrapper around `requestConnectedFrame` that only calls at most once in one frame
|
||||
*/
|
||||
export function requestThrottledConnectedFrame<
|
||||
T extends (...args: unknown[]) => void,
|
||||
>(func: T, element?: HTMLElement): T {
|
||||
let raqId: number | undefined = undefined;
|
||||
let latestArgs: unknown[] = [];
|
||||
|
||||
return ((...args: unknown[]) => {
|
||||
latestArgs = args;
|
||||
|
||||
if (raqId === undefined) {
|
||||
raqId = requestAnimationFrame(() => {
|
||||
raqId = undefined;
|
||||
|
||||
if (!element || element.isConnected) {
|
||||
func(...latestArgs);
|
||||
}
|
||||
});
|
||||
}
|
||||
}) as T;
|
||||
}
|
||||
|
||||
@requiredProperties({
|
||||
viewport: PropTypes.instanceOf(Viewport),
|
||||
})
|
||||
export class GfxViewportElement extends WithDisposable(ShadowlessElement) {
|
||||
static override styles = css`
|
||||
gfx-viewport {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
contain: size layout style;
|
||||
display: block;
|
||||
transform: none;
|
||||
}
|
||||
`;
|
||||
|
||||
private _hideOutsideBlock = requestThrottledConnectedFrame(() => {
|
||||
if (this.getModelsInViewport && this.host) {
|
||||
const host = this.host;
|
||||
const modelsInViewport = this.getModelsInViewport();
|
||||
|
||||
modelsInViewport.forEach(model => {
|
||||
const view = host.std.view.getBlock(model.id);
|
||||
|
||||
if (view) {
|
||||
view.style.display = '';
|
||||
}
|
||||
|
||||
if (this._lastVisibleModels?.has(model)) {
|
||||
this._lastVisibleModels!.delete(model);
|
||||
}
|
||||
});
|
||||
|
||||
this._lastVisibleModels?.forEach(model => {
|
||||
const view = host.std.view.getBlock(model.id);
|
||||
|
||||
if (view) {
|
||||
view.style.display = 'none';
|
||||
}
|
||||
});
|
||||
|
||||
this._lastVisibleModels = modelsInViewport;
|
||||
}
|
||||
}, this);
|
||||
|
||||
private _lastVisibleModels?: Set<GfxBlockElementModel>;
|
||||
|
||||
private _pendingChildrenUpdates: {
|
||||
id: string;
|
||||
resolve: () => void;
|
||||
}[] = [];
|
||||
|
||||
private _refreshViewport = requestThrottledConnectedFrame(() => {
|
||||
this._hideOutsideBlock();
|
||||
}, this);
|
||||
|
||||
private _updatingChildrenFlag = false;
|
||||
|
||||
renderingBlocks = new Set<string>();
|
||||
|
||||
override connectedCallback(): void {
|
||||
super.connectedCallback();
|
||||
|
||||
const viewportUpdateCallback = () => {
|
||||
this._refreshViewport();
|
||||
this._hideOutsideBlock();
|
||||
};
|
||||
|
||||
viewportUpdateCallback();
|
||||
this.disposables.add(
|
||||
this.viewport.viewportUpdated.on(() => viewportUpdateCallback())
|
||||
);
|
||||
this.disposables.add(
|
||||
this.viewport.sizeUpdated.on(() => viewportUpdateCallback())
|
||||
);
|
||||
}
|
||||
|
||||
override render() {
|
||||
return html``;
|
||||
}
|
||||
|
||||
scheduleUpdateChildren(id: string) {
|
||||
const { promise, resolve } = Promise.withResolvers<void>();
|
||||
|
||||
this._pendingChildrenUpdates.push({ id, resolve });
|
||||
|
||||
if (!this._updatingChildrenFlag) {
|
||||
this._updatingChildrenFlag = true;
|
||||
const schedule = () => {
|
||||
if (this._pendingChildrenUpdates.length) {
|
||||
const childToUpdates = this._pendingChildrenUpdates.splice(
|
||||
0,
|
||||
this.maxConcurrentRenders
|
||||
);
|
||||
|
||||
childToUpdates.forEach(({ resolve }) => resolve());
|
||||
|
||||
if (this._pendingChildrenUpdates.length) {
|
||||
requestAnimationFrame(() => {
|
||||
this.isConnected && schedule();
|
||||
});
|
||||
} else {
|
||||
this._updatingChildrenFlag = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
requestAnimationFrame(() => {
|
||||
this.isConnected && schedule();
|
||||
});
|
||||
}
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor getModelsInViewport: undefined | (() => Set<GfxBlockElementModel>);
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor host: undefined | EditorHost;
|
||||
|
||||
@property({ type: Number })
|
||||
accessor maxConcurrentRenders: number = 2;
|
||||
|
||||
@property({ attribute: false })
|
||||
accessor viewport!: Viewport;
|
||||
}
|
||||
413
blocksuite/framework/block-std/src/gfx/viewport.ts
Normal file
413
blocksuite/framework/block-std/src/gfx/viewport.ts
Normal file
@@ -0,0 +1,413 @@
|
||||
import {
|
||||
Bound,
|
||||
clamp,
|
||||
type IPoint,
|
||||
type IVec,
|
||||
Slot,
|
||||
Vec,
|
||||
} from '@blocksuite/global/utils';
|
||||
|
||||
function cutoff(value: number, ref: number, sign: number) {
|
||||
if (sign > 0 && value > ref) return ref;
|
||||
if (sign < 0 && value < ref) return ref;
|
||||
return value;
|
||||
}
|
||||
|
||||
export const ZOOM_MAX = 6.0;
|
||||
export const ZOOM_MIN = 0.1;
|
||||
|
||||
export class Viewport {
|
||||
private _cachedBoundingClientRect: DOMRect | null = null;
|
||||
|
||||
private _cachedOffsetWidth: number | null = null;
|
||||
|
||||
private _resizeObserver: ResizeObserver | null = null;
|
||||
|
||||
protected _center: IPoint = { x: 0, y: 0 };
|
||||
|
||||
protected _el: HTMLElement | null = null;
|
||||
|
||||
protected _height = 0;
|
||||
|
||||
protected _left = 0;
|
||||
|
||||
protected _locked = false;
|
||||
|
||||
protected _rafId: number | null = null;
|
||||
|
||||
protected _top = 0;
|
||||
|
||||
protected _width = 0;
|
||||
|
||||
protected _zoom: number = 1.0;
|
||||
|
||||
sizeUpdated = new Slot<{
|
||||
width: number;
|
||||
height: number;
|
||||
left: number;
|
||||
top: number;
|
||||
}>();
|
||||
|
||||
viewportMoved = new Slot<IVec>();
|
||||
|
||||
viewportUpdated = new Slot<{ zoom: number; center: IVec }>();
|
||||
|
||||
ZOOM_MAX = ZOOM_MAX;
|
||||
|
||||
ZOOM_MIN = ZOOM_MIN;
|
||||
|
||||
get boundingClientRect() {
|
||||
if (!this._el) return new DOMRect(0, 0, 0, 0);
|
||||
if (!this._cachedBoundingClientRect) {
|
||||
this._cachedBoundingClientRect = this._el.getBoundingClientRect();
|
||||
}
|
||||
return this._cachedBoundingClientRect;
|
||||
}
|
||||
|
||||
get center() {
|
||||
return this._center;
|
||||
}
|
||||
|
||||
get centerX() {
|
||||
return this._center.x;
|
||||
}
|
||||
|
||||
get centerY() {
|
||||
return this._center.y;
|
||||
}
|
||||
|
||||
get height() {
|
||||
return this.boundingClientRect.height;
|
||||
}
|
||||
|
||||
get left() {
|
||||
return this._left;
|
||||
}
|
||||
|
||||
// Does not allow the user to move and zoom the canvas in copilot tool
|
||||
get locked() {
|
||||
return this._locked;
|
||||
}
|
||||
|
||||
set locked(locked: boolean) {
|
||||
this._locked = locked;
|
||||
}
|
||||
|
||||
/**
|
||||
* Note this is different from the zoom property.
|
||||
* The editor itself may be scaled by outer container which is common in nested editor scenarios.
|
||||
* This property is used to calculate the scale of the editor.
|
||||
*/
|
||||
get scale() {
|
||||
if (!this._el || this._cachedOffsetWidth === null) return 1;
|
||||
return this.boundingClientRect.width / this._cachedOffsetWidth;
|
||||
}
|
||||
|
||||
get top() {
|
||||
return this._top;
|
||||
}
|
||||
|
||||
get translateX() {
|
||||
return -this.viewportX * this.zoom;
|
||||
}
|
||||
|
||||
get translateY() {
|
||||
return -this.viewportY * this.zoom;
|
||||
}
|
||||
|
||||
get viewportBounds() {
|
||||
const { viewportMinXY, viewportMaxXY } = this;
|
||||
|
||||
return Bound.from({
|
||||
...viewportMinXY,
|
||||
w: viewportMaxXY.x - viewportMinXY.x,
|
||||
h: viewportMaxXY.y - viewportMinXY.y,
|
||||
});
|
||||
}
|
||||
|
||||
get viewportMaxXY() {
|
||||
const { centerX, centerY, width, height, zoom } = this;
|
||||
return {
|
||||
x: centerX + width / 2 / zoom,
|
||||
y: centerY + height / 2 / zoom,
|
||||
};
|
||||
}
|
||||
|
||||
get viewportMinXY() {
|
||||
const { centerX, centerY, width, height, zoom } = this;
|
||||
return {
|
||||
x: centerX - width / 2 / zoom,
|
||||
y: centerY - height / 2 / zoom,
|
||||
};
|
||||
}
|
||||
|
||||
get viewportX() {
|
||||
const { centerX, width, zoom } = this;
|
||||
return centerX - width / 2 / zoom;
|
||||
}
|
||||
|
||||
get viewportY() {
|
||||
const { centerY, height, zoom } = this;
|
||||
return centerY - height / 2 / zoom;
|
||||
}
|
||||
|
||||
get width() {
|
||||
return this.boundingClientRect.width;
|
||||
}
|
||||
|
||||
get zoom() {
|
||||
return this._zoom;
|
||||
}
|
||||
|
||||
applyDeltaCenter(deltaX: number, deltaY: number) {
|
||||
this.setCenter(this.centerX + deltaX, this.centerY + deltaY);
|
||||
}
|
||||
|
||||
clearViewportElement() {
|
||||
if (this._resizeObserver && this._el) {
|
||||
this._resizeObserver.unobserve(this._el);
|
||||
this._resizeObserver.disconnect();
|
||||
}
|
||||
this._resizeObserver = null;
|
||||
this._el = null;
|
||||
this._cachedBoundingClientRect = null;
|
||||
this._cachedOffsetWidth = null;
|
||||
}
|
||||
|
||||
dispose() {
|
||||
this.clearViewportElement();
|
||||
this.sizeUpdated.dispose();
|
||||
this.viewportMoved.dispose();
|
||||
this.viewportUpdated.dispose();
|
||||
}
|
||||
|
||||
getFitToScreenData(
|
||||
bounds?: Bound | null,
|
||||
padding: [number, number, number, number] = [0, 0, 0, 0],
|
||||
maxZoom = ZOOM_MAX,
|
||||
fitToScreenPadding = 100
|
||||
) {
|
||||
let { centerX, centerY, zoom } = this;
|
||||
|
||||
if (!bounds) {
|
||||
return { zoom, centerX, centerY };
|
||||
}
|
||||
|
||||
const { x, y, w, h } = bounds;
|
||||
const [pt, pr, pb, pl] = padding;
|
||||
const { width, height } = this;
|
||||
|
||||
zoom = Math.min(
|
||||
(width - fitToScreenPadding - (pr + pl)) / w,
|
||||
(height - fitToScreenPadding - (pt + pb)) / h
|
||||
);
|
||||
zoom = clamp(zoom, ZOOM_MIN, clamp(maxZoom, ZOOM_MIN, ZOOM_MAX));
|
||||
|
||||
centerX = x + (w + pr / zoom) / 2 - pl / zoom / 2;
|
||||
centerY = y + (h + pb / zoom) / 2 - pt / zoom / 2;
|
||||
|
||||
return { zoom, centerX, centerY };
|
||||
}
|
||||
|
||||
isInViewport(bound: Bound) {
|
||||
const viewportBounds = Bound.from(this.viewportBounds);
|
||||
return (
|
||||
viewportBounds.contains(bound) ||
|
||||
viewportBounds.isIntersectWithBound(bound)
|
||||
);
|
||||
}
|
||||
|
||||
onResize() {
|
||||
if (!this._el) return;
|
||||
const { centerX, centerY, zoom, width: oldWidth, height: oldHeight } = this;
|
||||
const { left, top, width, height } = this.boundingClientRect;
|
||||
this._cachedOffsetWidth = this._el.offsetWidth;
|
||||
|
||||
this.setRect(left, top, width, height);
|
||||
this.setCenter(
|
||||
centerX - (oldWidth - width) / zoom / 2,
|
||||
centerY - (oldHeight - height) / zoom / 2
|
||||
);
|
||||
|
||||
this._width = width;
|
||||
this._height = height;
|
||||
}
|
||||
|
||||
setCenter(centerX: number, centerY: number) {
|
||||
this._center.x = centerX;
|
||||
this._center.y = centerY;
|
||||
this.viewportUpdated.emit({
|
||||
zoom: this.zoom,
|
||||
center: Vec.toVec(this.center) as IVec,
|
||||
});
|
||||
}
|
||||
|
||||
setRect(left: number, top: number, width: number, height: number) {
|
||||
this._left = left;
|
||||
this._top = top;
|
||||
this.sizeUpdated.emit({
|
||||
left,
|
||||
top,
|
||||
width,
|
||||
height,
|
||||
});
|
||||
}
|
||||
|
||||
setViewport(
|
||||
newZoom: number,
|
||||
newCenter = Vec.toVec(this.center),
|
||||
smooth = false
|
||||
) {
|
||||
const preZoom = this._zoom;
|
||||
if (smooth) {
|
||||
const cofficient = preZoom / newZoom;
|
||||
if (cofficient === 1) {
|
||||
this.smoothTranslate(newCenter[0], newCenter[1]);
|
||||
} else {
|
||||
const center = [this.centerX, this.centerY] as IVec;
|
||||
const focusPoint = Vec.mul(
|
||||
Vec.sub(newCenter, Vec.mul(center, cofficient)),
|
||||
1 / (1 - cofficient)
|
||||
);
|
||||
this.smoothZoom(newZoom, Vec.toPoint(focusPoint));
|
||||
}
|
||||
} else {
|
||||
this._center.x = newCenter[0];
|
||||
this._center.y = newCenter[1];
|
||||
this.setZoom(newZoom);
|
||||
}
|
||||
}
|
||||
|
||||
setViewportByBound(
|
||||
bound: Bound,
|
||||
padding: [number, number, number, number] = [0, 0, 0, 0],
|
||||
smooth = false
|
||||
) {
|
||||
const [pt, pr, pb, pl] = padding;
|
||||
const zoom = clamp(
|
||||
(this.width - (pr + pl)) / bound.w,
|
||||
this.ZOOM_MIN,
|
||||
(this.height - (pt + pb)) / bound.h
|
||||
);
|
||||
const center = [
|
||||
bound.x + (bound.w + pr / zoom) / 2 - pl / zoom / 2,
|
||||
bound.y + (bound.h + pb / zoom) / 2 - pt / zoom / 2,
|
||||
] as IVec;
|
||||
|
||||
this.setViewport(zoom, center, smooth);
|
||||
}
|
||||
|
||||
setViewportElement(el: HTMLElement) {
|
||||
this._el = el;
|
||||
this._cachedBoundingClientRect = el.getBoundingClientRect();
|
||||
this._cachedOffsetWidth = el.offsetWidth;
|
||||
|
||||
const { left, top, width, height } = this._cachedBoundingClientRect;
|
||||
this.setRect(left, top, width, height);
|
||||
|
||||
this._resizeObserver = new ResizeObserver(() => {
|
||||
this._cachedBoundingClientRect = null;
|
||||
this._cachedOffsetWidth = null;
|
||||
this.onResize();
|
||||
});
|
||||
this._resizeObserver.observe(el);
|
||||
}
|
||||
|
||||
setZoom(zoom: number, focusPoint?: IPoint) {
|
||||
const prevZoom = this.zoom;
|
||||
focusPoint = (focusPoint ?? this._center) as IPoint;
|
||||
this._zoom = clamp(zoom, this.ZOOM_MIN, this.ZOOM_MAX);
|
||||
const newZoom = this.zoom;
|
||||
|
||||
const offset = Vec.sub(Vec.toVec(this.center), Vec.toVec(focusPoint));
|
||||
const newCenter = Vec.add(
|
||||
Vec.toVec(focusPoint),
|
||||
Vec.mul(offset, prevZoom / newZoom)
|
||||
);
|
||||
this.setCenter(newCenter[0], newCenter[1]);
|
||||
this.viewportUpdated.emit({
|
||||
zoom: this.zoom,
|
||||
center: Vec.toVec(this.center) as IVec,
|
||||
});
|
||||
}
|
||||
|
||||
smoothTranslate(x: number, y: number) {
|
||||
const { center } = this;
|
||||
const delta = { x: x - center.x, y: y - center.y };
|
||||
const innerSmoothTranslate = () => {
|
||||
if (this._rafId) cancelAnimationFrame(this._rafId);
|
||||
this._rafId = requestAnimationFrame(() => {
|
||||
const rate = 10;
|
||||
const step = { x: delta.x / rate, y: delta.y / rate };
|
||||
const nextCenter = {
|
||||
x: this.centerX + step.x,
|
||||
y: this.centerY + step.y,
|
||||
};
|
||||
const signX = delta.x > 0 ? 1 : -1;
|
||||
const signY = delta.y > 0 ? 1 : -1;
|
||||
nextCenter.x = cutoff(nextCenter.x, x, signX);
|
||||
nextCenter.y = cutoff(nextCenter.y, y, signY);
|
||||
this.setCenter(nextCenter.x, nextCenter.y);
|
||||
|
||||
if (nextCenter.x != x || nextCenter.y != y) innerSmoothTranslate();
|
||||
});
|
||||
};
|
||||
innerSmoothTranslate();
|
||||
}
|
||||
|
||||
smoothZoom(zoom: number, focusPoint?: IPoint) {
|
||||
const delta = zoom - this.zoom;
|
||||
if (this._rafId) cancelAnimationFrame(this._rafId);
|
||||
|
||||
const innerSmoothZoom = () => {
|
||||
this._rafId = requestAnimationFrame(() => {
|
||||
const sign = delta > 0 ? 1 : -1;
|
||||
const total = 10;
|
||||
const step = delta / total;
|
||||
const nextZoom = cutoff(this.zoom + step, zoom, sign);
|
||||
|
||||
this.setZoom(nextZoom, focusPoint);
|
||||
|
||||
if (nextZoom != zoom) innerSmoothZoom();
|
||||
});
|
||||
};
|
||||
innerSmoothZoom();
|
||||
}
|
||||
|
||||
toModelBound(bound: Bound) {
|
||||
const { w, h } = bound;
|
||||
const [x, y] = this.toModelCoord(bound.x, bound.y);
|
||||
|
||||
return new Bound(x, y, w / this.zoom, h / this.zoom);
|
||||
}
|
||||
|
||||
toModelCoord(viewX: number, viewY: number): IVec {
|
||||
const { viewportX, viewportY, zoom, scale } = this;
|
||||
return [viewportX + viewX / zoom / scale, viewportY + viewY / zoom / scale];
|
||||
}
|
||||
|
||||
toModelCoordFromClientCoord([x, y]: IVec): IVec {
|
||||
const { left, top } = this;
|
||||
return this.toModelCoord(x - left, y - top);
|
||||
}
|
||||
|
||||
toViewBound(bound: Bound) {
|
||||
const { w, h } = bound;
|
||||
const [x, y] = this.toViewCoord(bound.x, bound.y);
|
||||
|
||||
return new Bound(x, y, w * this.zoom, h * this.zoom);
|
||||
}
|
||||
|
||||
toViewCoord(modelX: number, modelY: number): IVec {
|
||||
const { viewportX, viewportY, zoom, scale } = this;
|
||||
return [
|
||||
(modelX - viewportX) * zoom * scale,
|
||||
(modelY - viewportY) * zoom * scale,
|
||||
];
|
||||
}
|
||||
|
||||
toViewCoordFromClientCoord([x, y]: IVec): IVec {
|
||||
const { left, top } = this;
|
||||
return [x - left, y - top];
|
||||
}
|
||||
}
|
||||
38
blocksuite/framework/block-std/src/identifier.ts
Normal file
38
blocksuite/framework/block-std/src/identifier.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { createIdentifier } from '@blocksuite/global/di';
|
||||
|
||||
import type { Command } from './command/index.js';
|
||||
import type { EventOptions, UIEventHandler } from './event/index.js';
|
||||
import type { BlockService, LifeCycleWatcher } from './extension/index.js';
|
||||
import type { BlockStdScope } from './scope/index.js';
|
||||
import type { SelectionConstructor } from './selection/index.js';
|
||||
import type { BlockViewType, WidgetViewMapType } from './spec/type.js';
|
||||
|
||||
export const BlockServiceIdentifier =
|
||||
createIdentifier<BlockService>('BlockService');
|
||||
|
||||
export const BlockFlavourIdentifier = createIdentifier<{ flavour: string }>(
|
||||
'BlockFlavour'
|
||||
);
|
||||
|
||||
export const CommandIdentifier = createIdentifier<Command>('Commands');
|
||||
|
||||
export const ConfigIdentifier =
|
||||
createIdentifier<Record<string, unknown>>('Config');
|
||||
|
||||
export const BlockViewIdentifier = createIdentifier<BlockViewType>('BlockView');
|
||||
|
||||
export const WidgetViewMapIdentifier =
|
||||
createIdentifier<WidgetViewMapType>('WidgetViewMap');
|
||||
|
||||
export const LifeCycleWatcherIdentifier =
|
||||
createIdentifier<LifeCycleWatcher>('LifeCycleWatcher');
|
||||
|
||||
export const StdIdentifier = createIdentifier<BlockStdScope>('Std');
|
||||
|
||||
export const KeymapIdentifier = createIdentifier<{
|
||||
getter: (std: BlockStdScope) => Record<string, UIEventHandler>;
|
||||
options?: EventOptions;
|
||||
}>('Keymap');
|
||||
|
||||
export const SelectionIdentifier =
|
||||
createIdentifier<SelectionConstructor>('Selection');
|
||||
12
blocksuite/framework/block-std/src/index.ts
Normal file
12
blocksuite/framework/block-std/src/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export * from './clipboard/index.js';
|
||||
export * from './command/index.js';
|
||||
export * from './event/index.js';
|
||||
export * from './extension/index.js';
|
||||
export * from './identifier.js';
|
||||
export * from './range/index.js';
|
||||
export * from './scope/index.js';
|
||||
export * from './selection/index.js';
|
||||
export * from './service/index.js';
|
||||
export * from './spec/index.js';
|
||||
export * from './utils/index.js';
|
||||
export * from './view/index.js';
|
||||
9
blocksuite/framework/block-std/src/range/consts.ts
Normal file
9
blocksuite/framework/block-std/src/range/consts.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Used to exclude certain elements when using `getSelectedBlockComponentsByRange`.
|
||||
*/
|
||||
export const RANGE_QUERY_EXCLUDE_ATTR = 'data-range-query-exclude';
|
||||
|
||||
/**
|
||||
* Used to mark certain elements so that they are excluded when synchronizing the native range and text selection (such as database block).
|
||||
*/
|
||||
export const RANGE_SYNC_EXCLUDE_ATTR = 'data-range-sync-exclude';
|
||||
4
blocksuite/framework/block-std/src/range/index.ts
Normal file
4
blocksuite/framework/block-std/src/range/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './consts.js';
|
||||
export * from './inline-range-provider.js';
|
||||
export * from './range-binding.js';
|
||||
export * from './range-manager.js';
|
||||
@@ -0,0 +1,104 @@
|
||||
import type { InlineRange, InlineRangeProvider } from '@blocksuite/inline';
|
||||
import { signal } from '@preact/signals-core';
|
||||
|
||||
import type { TextSelection } from '../selection/index.js';
|
||||
import type { BlockComponent } from '../view/element/block-component.js';
|
||||
|
||||
export const getInlineRangeProvider: (
|
||||
element: BlockComponent
|
||||
) => InlineRangeProvider | null = element => {
|
||||
const editorHost = element.host;
|
||||
const selectionManager = editorHost.selection;
|
||||
const rangeManager = editorHost.range;
|
||||
|
||||
if (!selectionManager || !rangeManager) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const calculateInlineRange = (
|
||||
range: Range,
|
||||
textSelection: TextSelection
|
||||
): InlineRange | null => {
|
||||
const { from, to } = textSelection;
|
||||
|
||||
if (from.blockId === element.blockId) {
|
||||
return {
|
||||
index: from.index,
|
||||
length: from.length,
|
||||
};
|
||||
}
|
||||
|
||||
if (to && to.blockId === element.blockId) {
|
||||
return {
|
||||
index: to.index,
|
||||
length: to.length,
|
||||
};
|
||||
}
|
||||
|
||||
if (!element.model.text) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const elementRange = rangeManager.textSelectionToRange(
|
||||
selectionManager.create('text', {
|
||||
from: {
|
||||
index: 0,
|
||||
blockId: element.blockId,
|
||||
length: element.model.text.length,
|
||||
},
|
||||
to: null,
|
||||
})
|
||||
);
|
||||
|
||||
if (
|
||||
elementRange &&
|
||||
elementRange.compareBoundaryPoints(Range.START_TO_START, range) > -1 &&
|
||||
elementRange.compareBoundaryPoints(Range.END_TO_END, range) < 1
|
||||
) {
|
||||
return {
|
||||
index: 0,
|
||||
length: element.model.text.length,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const setInlineRange = (inlineRange: InlineRange | null) => {
|
||||
// skip `setInlineRange` from `inlineEditor` when composing happens across blocks,
|
||||
// selection will be updated in `range-binding`
|
||||
if (rangeManager.binding?.isComposing) return;
|
||||
|
||||
if (!inlineRange) {
|
||||
selectionManager.clear(['text']);
|
||||
} else {
|
||||
const textSelection = selectionManager.create('text', {
|
||||
from: {
|
||||
blockId: element.blockId,
|
||||
index: inlineRange.index,
|
||||
length: inlineRange.length,
|
||||
},
|
||||
to: null,
|
||||
});
|
||||
selectionManager.setGroup('note', [textSelection]);
|
||||
}
|
||||
};
|
||||
const inlineRange$: InlineRangeProvider['inlineRange$'] = signal(null);
|
||||
selectionManager.slots.changed.on(selections => {
|
||||
const textSelection = selections.find(s => s.type === 'text') as
|
||||
| TextSelection
|
||||
| undefined;
|
||||
const range = rangeManager.value;
|
||||
if (!range || !textSelection) {
|
||||
inlineRange$.value = null;
|
||||
return;
|
||||
}
|
||||
const inlineRange = calculateInlineRange(range, textSelection);
|
||||
inlineRange$.value = inlineRange;
|
||||
});
|
||||
|
||||
return {
|
||||
setInlineRange,
|
||||
inlineRange$,
|
||||
};
|
||||
};
|
||||
343
blocksuite/framework/block-std/src/range/range-binding.ts
Normal file
343
blocksuite/framework/block-std/src/range/range-binding.ts
Normal file
@@ -0,0 +1,343 @@
|
||||
import { throttle } from '@blocksuite/global/utils';
|
||||
import type { BlockModel } from '@blocksuite/store';
|
||||
|
||||
import type { BaseSelection, TextSelection } from '../selection/index.js';
|
||||
import type { BlockComponent } from '../view/element/block-component.js';
|
||||
import { BLOCK_ID_ATTR } from '../view/index.js';
|
||||
import { RANGE_SYNC_EXCLUDE_ATTR } from './consts.js';
|
||||
import type { RangeManager } from './range-manager.js';
|
||||
|
||||
/**
|
||||
* Two-way binding between native range and text selection
|
||||
*/
|
||||
export class RangeBinding {
|
||||
private _compositionStartCallback:
|
||||
| ((event: CompositionEvent) => Promise<void>)
|
||||
| null = null;
|
||||
|
||||
private _computePath = (modelId: string) => {
|
||||
const block = this.host.std.doc.getBlock(modelId)?.model;
|
||||
if (!block) return [];
|
||||
|
||||
const path: string[] = [];
|
||||
let parent: BlockModel | null = block;
|
||||
while (parent) {
|
||||
path.unshift(parent.id);
|
||||
parent = this.host.doc.getParent(parent);
|
||||
}
|
||||
|
||||
return path;
|
||||
};
|
||||
|
||||
private _onBeforeInput = (event: InputEvent) => {
|
||||
const selection = this.selectionManager.find('text');
|
||||
if (!selection) return;
|
||||
|
||||
if (event.isComposing) return;
|
||||
|
||||
const { from, to } = selection;
|
||||
if (!to || from.blockId === to.blockId) return;
|
||||
|
||||
const range = this.rangeManager?.value;
|
||||
if (!range) return;
|
||||
|
||||
const blocks = this.rangeManager.getSelectedBlockComponentsByRange(range, {
|
||||
mode: 'flat',
|
||||
});
|
||||
|
||||
const start = blocks.at(0);
|
||||
const end = blocks.at(-1);
|
||||
if (!start || !end) return;
|
||||
|
||||
const startText = start.model.text;
|
||||
const endText = end.model.text;
|
||||
if (!startText || !endText) return;
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
this.host.doc.transact(() => {
|
||||
startText.delete(from.index, from.length);
|
||||
startText.insert(event.data ?? '', from.index);
|
||||
endText.delete(0, to.length);
|
||||
startText.join(endText);
|
||||
|
||||
blocks
|
||||
.slice(1)
|
||||
// delete from lowest to highest
|
||||
.reverse()
|
||||
.forEach(block => {
|
||||
const parent = this.host.doc.getParent(block.model);
|
||||
if (!parent) return;
|
||||
this.host.doc.deleteBlock(block.model, {
|
||||
bringChildrenTo: parent,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
const newSelection = this.selectionManager.create('text', {
|
||||
from: {
|
||||
blockId: from.blockId,
|
||||
index: from.index + (event.data?.length ?? 0),
|
||||
length: 0,
|
||||
},
|
||||
to: null,
|
||||
});
|
||||
this.selectionManager.setGroup('note', [newSelection]);
|
||||
};
|
||||
|
||||
private _onCompositionEnd = (event: CompositionEvent) => {
|
||||
if (this._compositionStartCallback) {
|
||||
event.preventDefault();
|
||||
event.stopPropagation();
|
||||
this._compositionStartCallback(event).catch(console.error);
|
||||
this._compositionStartCallback = null;
|
||||
}
|
||||
};
|
||||
|
||||
private _onCompositionStart = () => {
|
||||
const selection = this.selectionManager.find('text');
|
||||
if (!selection) return;
|
||||
|
||||
const { from, to } = selection;
|
||||
if (!to) return;
|
||||
|
||||
this.isComposing = true;
|
||||
|
||||
const range = this.rangeManager?.value;
|
||||
if (!range) return;
|
||||
|
||||
const blocks = this.rangeManager.getSelectedBlockComponentsByRange(range, {
|
||||
mode: 'flat',
|
||||
});
|
||||
|
||||
const start = blocks.at(0);
|
||||
const end = blocks.at(-1);
|
||||
if (!start || !end) return;
|
||||
|
||||
const startText = start.model.text;
|
||||
const endText = end.model.text;
|
||||
if (!startText || !endText) return;
|
||||
|
||||
this._compositionStartCallback = async event => {
|
||||
this.isComposing = false;
|
||||
|
||||
this.host.renderRoot.replaceChildren();
|
||||
// Because we bypassed Lit and disrupted the DOM structure, this will cause an inconsistency in the original state of `ChildPart`.
|
||||
// Therefore, we need to remove the original `ChildPart`.
|
||||
// https://github.com/lit/lit/blob/a2cd76cfdea4ed717362bb1db32710d70550469d/packages/lit-html/src/lit-html.ts#L2248
|
||||
|
||||
delete (this.host.renderRoot as any)['_$litPart$'];
|
||||
this.host.requestUpdate();
|
||||
await this.host.updateComplete;
|
||||
|
||||
this.host.doc.captureSync();
|
||||
|
||||
this.host.doc.transact(() => {
|
||||
endText.delete(0, to.length);
|
||||
startText.delete(from.index, from.length);
|
||||
startText.insert(event.data, from.index);
|
||||
startText.join(endText);
|
||||
|
||||
blocks
|
||||
.slice(1)
|
||||
// delete from lowest to highest
|
||||
.reverse()
|
||||
.forEach(block => {
|
||||
const parent = this.host.doc.getParent(block.model);
|
||||
if (!parent) return;
|
||||
this.host.doc.deleteBlock(block.model, {
|
||||
bringChildrenTo: parent,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
await this.host.updateComplete;
|
||||
|
||||
const selection = this.selectionManager.create('text', {
|
||||
from: {
|
||||
blockId: from.blockId,
|
||||
index: from.index + (event.data?.length ?? 0),
|
||||
length: 0,
|
||||
},
|
||||
to: null,
|
||||
});
|
||||
this.host.selection.setGroup('note', [selection]);
|
||||
this.rangeManager?.syncTextSelectionToRange(selection);
|
||||
};
|
||||
};
|
||||
|
||||
private _onNativeSelectionChanged = async () => {
|
||||
if (this.isComposing) return;
|
||||
if (!this.host) return; // Unstable when switching views, card <-> embed
|
||||
|
||||
await this.host.updateComplete;
|
||||
|
||||
const selection = document.getSelection();
|
||||
if (!selection) {
|
||||
this.selectionManager.clear(['text']);
|
||||
return;
|
||||
}
|
||||
const range = selection.rangeCount > 0 ? selection.getRangeAt(0) : null;
|
||||
|
||||
if (!range) {
|
||||
this._prevTextSelection = null;
|
||||
this.selectionManager.clear(['text']);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this.host.contains(range.commonAncestorContainer)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// range is in a non-editable element
|
||||
// ex. placeholder
|
||||
const isRangeOutNotEditable =
|
||||
range.startContainer instanceof HTMLElement &&
|
||||
range.startContainer.contentEditable === 'false' &&
|
||||
range.endContainer instanceof HTMLElement &&
|
||||
range.endContainer.contentEditable === 'false';
|
||||
if (isRangeOutNotEditable) {
|
||||
this._prevTextSelection = null;
|
||||
this.selectionManager.clear(['text']);
|
||||
|
||||
// force clear native selection to break inline editor input
|
||||
selection.removeRange(range);
|
||||
return;
|
||||
}
|
||||
|
||||
const el =
|
||||
range.commonAncestorContainer instanceof Element
|
||||
? range.commonAncestorContainer
|
||||
: range.commonAncestorContainer.parentElement;
|
||||
if (!el) return;
|
||||
const block = el.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
|
||||
if (block?.getAttribute(RANGE_SYNC_EXCLUDE_ATTR) === 'true') return;
|
||||
|
||||
const inlineEditor = this.rangeManager?.getClosestInlineEditor(
|
||||
range.commonAncestorContainer
|
||||
);
|
||||
if (inlineEditor?.isComposing) return;
|
||||
|
||||
const isRangeReversed =
|
||||
!!selection.anchorNode &&
|
||||
!!selection.focusNode &&
|
||||
(selection.anchorNode === selection.focusNode
|
||||
? selection.anchorOffset > selection.focusOffset
|
||||
: selection.anchorNode.compareDocumentPosition(selection.focusNode) ===
|
||||
Node.DOCUMENT_POSITION_PRECEDING);
|
||||
const textSelection = this.rangeManager?.rangeToTextSelection(
|
||||
range,
|
||||
isRangeReversed
|
||||
);
|
||||
if (!textSelection) {
|
||||
this._prevTextSelection = null;
|
||||
this.selectionManager.clear(['text']);
|
||||
return;
|
||||
}
|
||||
|
||||
const model = this.host.doc.getBlockById(textSelection.blockId);
|
||||
// If the model is not found, the selection maybe in another editor
|
||||
if (!model) return;
|
||||
|
||||
this._prevTextSelection = {
|
||||
selection: textSelection,
|
||||
path: this._computePath(model.id),
|
||||
};
|
||||
this.rangeManager?.syncRangeToTextSelection(range, isRangeReversed);
|
||||
};
|
||||
|
||||
private _onStdSelectionChanged = (selections: BaseSelection[]) => {
|
||||
const text =
|
||||
selections.find((selection): selection is TextSelection =>
|
||||
selection.is('text')
|
||||
) ?? null;
|
||||
|
||||
if (text === this._prevTextSelection) {
|
||||
return;
|
||||
}
|
||||
// wait for lit updated
|
||||
this.host.updateComplete
|
||||
.then(() => {
|
||||
const id = text?.blockId;
|
||||
const path = id && this._computePath(id);
|
||||
|
||||
if (this.host.event.active) {
|
||||
const eq =
|
||||
text && this._prevTextSelection && path
|
||||
? text.equals(this._prevTextSelection.selection) &&
|
||||
path.join('') === this._prevTextSelection.path.join('')
|
||||
: false;
|
||||
|
||||
if (eq) return;
|
||||
}
|
||||
|
||||
this._prevTextSelection =
|
||||
text && path
|
||||
? {
|
||||
selection: text,
|
||||
path,
|
||||
}
|
||||
: null;
|
||||
if (text) {
|
||||
this.rangeManager?.syncTextSelectionToRange(text);
|
||||
} else {
|
||||
this.rangeManager?.clear();
|
||||
}
|
||||
})
|
||||
.catch(console.error);
|
||||
};
|
||||
|
||||
private _prevTextSelection: {
|
||||
selection: TextSelection;
|
||||
path: string[];
|
||||
} | null = null;
|
||||
|
||||
isComposing = false;
|
||||
|
||||
get host() {
|
||||
return this.manager.std.host;
|
||||
}
|
||||
|
||||
get rangeManager() {
|
||||
return this.host.range;
|
||||
}
|
||||
|
||||
get selectionManager() {
|
||||
return this.host.selection;
|
||||
}
|
||||
|
||||
constructor(public manager: RangeManager) {
|
||||
this.host.disposables.add(
|
||||
this.selectionManager.slots.changed.on(this._onStdSelectionChanged)
|
||||
);
|
||||
|
||||
this.host.disposables.addFromEvent(
|
||||
document,
|
||||
'selectionchange',
|
||||
throttle(() => {
|
||||
this._onNativeSelectionChanged().catch(console.error);
|
||||
}, 10)
|
||||
);
|
||||
|
||||
this.host.disposables.add(
|
||||
this.host.event.add('beforeInput', ctx => {
|
||||
const event = ctx.get('defaultState').event as InputEvent;
|
||||
this._onBeforeInput(event);
|
||||
})
|
||||
);
|
||||
|
||||
this.host.disposables.addFromEvent(
|
||||
this.host,
|
||||
'compositionstart',
|
||||
this._onCompositionStart
|
||||
);
|
||||
this.host.disposables.addFromEvent(
|
||||
this.host,
|
||||
'compositionend',
|
||||
this._onCompositionEnd,
|
||||
{
|
||||
capture: true,
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
254
blocksuite/framework/block-std/src/range/range-manager.ts
Normal file
254
blocksuite/framework/block-std/src/range/range-manager.ts
Normal file
@@ -0,0 +1,254 @@
|
||||
import { INLINE_ROOT_ATTR, type InlineRootElement } from '@blocksuite/inline';
|
||||
|
||||
import { LifeCycleWatcher } from '../extension/index.js';
|
||||
import type { TextSelection } from '../selection/index.js';
|
||||
import type { BlockComponent } from '../view/element/block-component.js';
|
||||
import { BLOCK_ID_ATTR } from '../view/index.js';
|
||||
import { RANGE_QUERY_EXCLUDE_ATTR, RANGE_SYNC_EXCLUDE_ATTR } from './consts.js';
|
||||
import { RangeBinding } from './range-binding.js';
|
||||
|
||||
/**
|
||||
* CRUD for Range and TextSelection
|
||||
*/
|
||||
export class RangeManager extends LifeCycleWatcher {
|
||||
static override readonly key = 'rangeManager';
|
||||
|
||||
binding: RangeBinding | null = null;
|
||||
|
||||
get value() {
|
||||
const selection = document.getSelection();
|
||||
if (!selection) {
|
||||
return;
|
||||
}
|
||||
if (selection.rangeCount === 0) return null;
|
||||
return selection.getRangeAt(0);
|
||||
}
|
||||
|
||||
private _isRangeSyncExcluded(el: Element) {
|
||||
return !!el.closest(`[${RANGE_SYNC_EXCLUDE_ATTR}="true"]`);
|
||||
}
|
||||
|
||||
clear() {
|
||||
const selection = document.getSelection();
|
||||
if (!selection) return;
|
||||
selection.removeAllRanges();
|
||||
|
||||
const topContenteditableElement = this.std.host.querySelector(
|
||||
'[contenteditable="true"]'
|
||||
);
|
||||
if (topContenteditableElement instanceof HTMLElement) {
|
||||
topContenteditableElement.blur();
|
||||
}
|
||||
if (document.activeElement instanceof HTMLElement) {
|
||||
document.activeElement.blur();
|
||||
}
|
||||
}
|
||||
|
||||
getClosestBlock(node: Node) {
|
||||
const el = node instanceof Element ? node : node.parentElement;
|
||||
if (!el) return null;
|
||||
const block = el.closest<BlockComponent>(`[${BLOCK_ID_ATTR}]`);
|
||||
if (!block) return null;
|
||||
if (this._isRangeSyncExcluded(block)) return null;
|
||||
return block;
|
||||
}
|
||||
|
||||
getClosestInlineEditor(node: Node) {
|
||||
const el = node instanceof Element ? node : node.parentElement;
|
||||
if (!el) return null;
|
||||
const inlineRoot = el.closest<InlineRootElement>(`[${INLINE_ROOT_ATTR}]`);
|
||||
if (!inlineRoot) return null;
|
||||
|
||||
if (this._isRangeSyncExcluded(inlineRoot)) return null;
|
||||
|
||||
return inlineRoot.inlineEditor;
|
||||
}
|
||||
|
||||
/**
|
||||
* @example
|
||||
* aaa
|
||||
* b[bb
|
||||
* ccc
|
||||
* ddd
|
||||
* ee]e
|
||||
*
|
||||
* all mode: [aaa, bbb, ccc, ddd, eee]
|
||||
* flat mode: [bbb, ccc, ddd, eee]
|
||||
* highest mode: [bbb, ddd]
|
||||
*
|
||||
* match function will be evaluated before filtering using mode
|
||||
*/
|
||||
getSelectedBlockComponentsByRange(
|
||||
range: Range,
|
||||
options: {
|
||||
match?: (el: BlockComponent) => boolean;
|
||||
mode?: 'all' | 'flat' | 'highest';
|
||||
} = {}
|
||||
): BlockComponent[] {
|
||||
const { mode = 'all', match = () => true } = options;
|
||||
|
||||
let result = Array.from<BlockComponent>(
|
||||
this.std.host.querySelectorAll(
|
||||
`[${BLOCK_ID_ATTR}]:not([${RANGE_QUERY_EXCLUDE_ATTR}="true"])`
|
||||
)
|
||||
).filter(el => range.intersectsNode(el) && match(el));
|
||||
|
||||
if (result.length === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const firstElement = this.getClosestBlock(range.startContainer);
|
||||
if (!firstElement) return [];
|
||||
const firstElementIndex = result.indexOf(firstElement);
|
||||
if (firstElementIndex === -1) return [];
|
||||
|
||||
if (mode === 'flat') {
|
||||
result = result.slice(firstElementIndex);
|
||||
} else if (mode === 'highest') {
|
||||
result = result.slice(firstElementIndex);
|
||||
let parent = result[0];
|
||||
result = result.filter((node, index) => {
|
||||
if (index === 0) return true;
|
||||
if (
|
||||
parent.compareDocumentPosition(node) &
|
||||
Node.DOCUMENT_POSITION_CONTAINED_BY
|
||||
) {
|
||||
return false;
|
||||
} else {
|
||||
parent = node;
|
||||
return true;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
override mounted() {
|
||||
this.binding = new RangeBinding(this);
|
||||
}
|
||||
|
||||
queryInlineEditorByPath(path: string) {
|
||||
const block = this.std.host.view.getBlock(path);
|
||||
if (!block) return null;
|
||||
|
||||
const inlineRoot = block.querySelector<InlineRootElement>(
|
||||
`[${INLINE_ROOT_ATTR}]`
|
||||
);
|
||||
if (!inlineRoot) return null;
|
||||
|
||||
if (this._isRangeSyncExcluded(inlineRoot)) return null;
|
||||
|
||||
return inlineRoot.inlineEditor;
|
||||
}
|
||||
|
||||
rangeToTextSelection(range: Range, reverse = false): TextSelection | null {
|
||||
const { startContainer, endContainer } = range;
|
||||
|
||||
const startBlock = this.getClosestBlock(startContainer);
|
||||
const endBlock = this.getClosestBlock(endContainer);
|
||||
if (!startBlock || !endBlock) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const startInlineEditor = this.getClosestInlineEditor(startContainer);
|
||||
const endInlineEditor = this.getClosestInlineEditor(endContainer);
|
||||
if (!startInlineEditor || !endInlineEditor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const startInlineRange = startInlineEditor.toInlineRange(range);
|
||||
const endInlineRange = endInlineEditor.toInlineRange(range);
|
||||
if (!startInlineRange || !endInlineRange) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this.std.host.selection.create('text', {
|
||||
from: {
|
||||
blockId: startBlock.blockId,
|
||||
index: startInlineRange.index,
|
||||
length: startInlineRange.length,
|
||||
},
|
||||
to:
|
||||
startBlock === endBlock
|
||||
? null
|
||||
: {
|
||||
blockId: endBlock.blockId,
|
||||
index: endInlineRange.index,
|
||||
length: endInlineRange.length,
|
||||
},
|
||||
reverse,
|
||||
});
|
||||
}
|
||||
|
||||
set(range: Range) {
|
||||
const selection = document.getSelection();
|
||||
if (!selection) return;
|
||||
selection.removeAllRanges();
|
||||
selection.addRange(range);
|
||||
}
|
||||
|
||||
syncRangeToTextSelection(range: Range, isRangeReversed: boolean) {
|
||||
const selectionManager = this.std.host.selection;
|
||||
|
||||
if (!range) {
|
||||
selectionManager.clear(['text']);
|
||||
return;
|
||||
}
|
||||
|
||||
const textSelection = this.rangeToTextSelection(range, isRangeReversed);
|
||||
if (textSelection) {
|
||||
selectionManager.setGroup('note', [textSelection]);
|
||||
} else {
|
||||
selectionManager.clear(['text']);
|
||||
}
|
||||
}
|
||||
|
||||
syncTextSelectionToRange(selection: TextSelection) {
|
||||
const range = this.textSelectionToRange(selection);
|
||||
if (range) {
|
||||
this.set(range);
|
||||
} else {
|
||||
this.clear();
|
||||
}
|
||||
}
|
||||
|
||||
textSelectionToRange(selection: TextSelection): Range | null {
|
||||
const { from, to } = selection;
|
||||
|
||||
const fromInlineEditor = this.queryInlineEditorByPath(from.blockId);
|
||||
if (!fromInlineEditor) return null;
|
||||
|
||||
if (selection.isInSameBlock()) {
|
||||
return fromInlineEditor.toDomRange({
|
||||
index: from.index,
|
||||
length: from.length,
|
||||
});
|
||||
}
|
||||
|
||||
if (!to) return null;
|
||||
const toInlineEditor = this.queryInlineEditorByPath(to.blockId);
|
||||
if (!toInlineEditor) return null;
|
||||
|
||||
const fromRange = fromInlineEditor.toDomRange({
|
||||
index: from.index,
|
||||
length: from.length,
|
||||
});
|
||||
const toRange = toInlineEditor.toDomRange({
|
||||
index: to.index,
|
||||
length: to.length,
|
||||
});
|
||||
|
||||
if (!fromRange || !toRange) return null;
|
||||
|
||||
const range = document.createRange();
|
||||
const startContainer = fromRange.startContainer;
|
||||
const startOffset = fromRange.startOffset;
|
||||
const endContainer = toRange.endContainer;
|
||||
const endOffset = toRange.endOffset;
|
||||
range.setStart(startContainer, startOffset);
|
||||
range.setEnd(endContainer, endOffset);
|
||||
|
||||
return range;
|
||||
}
|
||||
}
|
||||
207
blocksuite/framework/block-std/src/scope/block-std-scope.ts
Normal file
207
blocksuite/framework/block-std/src/scope/block-std-scope.ts
Normal file
@@ -0,0 +1,207 @@
|
||||
import type { ServiceProvider } from '@blocksuite/global/di';
|
||||
import { Container } from '@blocksuite/global/di';
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import type { Doc } from '@blocksuite/store';
|
||||
|
||||
import { Clipboard } from '../clipboard/index.js';
|
||||
import { CommandManager } from '../command/index.js';
|
||||
import { UIEventDispatcher } from '../event/index.js';
|
||||
import type { BlockService, ExtensionType } from '../extension/index.js';
|
||||
import { GfxController } from '../gfx/controller.js';
|
||||
import { GfxSelectionManager } from '../gfx/selection.js';
|
||||
import { SurfaceMiddlewareExtension } from '../gfx/surface-middleware.js';
|
||||
import { ViewManager } from '../gfx/view/view-manager.js';
|
||||
import {
|
||||
BlockServiceIdentifier,
|
||||
BlockViewIdentifier,
|
||||
ConfigIdentifier,
|
||||
LifeCycleWatcherIdentifier,
|
||||
StdIdentifier,
|
||||
} from '../identifier.js';
|
||||
import { RangeManager } from '../range/index.js';
|
||||
import {
|
||||
BlockSelectionExtension,
|
||||
CursorSelectionExtension,
|
||||
SelectionManager,
|
||||
SurfaceSelectionExtension,
|
||||
TextSelectionExtension,
|
||||
} from '../selection/index.js';
|
||||
import { ServiceManager } from '../service/index.js';
|
||||
import { EditorHost } from '../view/element/index.js';
|
||||
import { ViewStore } from '../view/view-store.js';
|
||||
|
||||
export interface BlockStdOptions {
|
||||
doc: Doc;
|
||||
extensions: ExtensionType[];
|
||||
}
|
||||
|
||||
const internalExtensions = [
|
||||
ServiceManager,
|
||||
CommandManager,
|
||||
UIEventDispatcher,
|
||||
SelectionManager,
|
||||
RangeManager,
|
||||
ViewStore,
|
||||
Clipboard,
|
||||
GfxController,
|
||||
BlockSelectionExtension,
|
||||
TextSelectionExtension,
|
||||
SurfaceSelectionExtension,
|
||||
CursorSelectionExtension,
|
||||
GfxSelectionManager,
|
||||
SurfaceMiddlewareExtension,
|
||||
ViewManager,
|
||||
];
|
||||
|
||||
export class BlockStdScope {
|
||||
static internalExtensions = internalExtensions;
|
||||
|
||||
private _getHost: () => EditorHost;
|
||||
|
||||
readonly container: Container;
|
||||
|
||||
readonly doc: Doc;
|
||||
|
||||
readonly provider: ServiceProvider;
|
||||
|
||||
readonly userExtensions: ExtensionType[];
|
||||
|
||||
private get _lifeCycleWatchers() {
|
||||
return this.provider.getAll(LifeCycleWatcherIdentifier);
|
||||
}
|
||||
|
||||
get clipboard() {
|
||||
return this.get(Clipboard);
|
||||
}
|
||||
|
||||
get collection() {
|
||||
return this.doc.collection;
|
||||
}
|
||||
|
||||
get command() {
|
||||
return this.get(CommandManager);
|
||||
}
|
||||
|
||||
get event() {
|
||||
return this.get(UIEventDispatcher);
|
||||
}
|
||||
|
||||
get get() {
|
||||
return this.provider.get.bind(this.provider);
|
||||
}
|
||||
|
||||
get getOptional() {
|
||||
return this.provider.getOptional.bind(this.provider);
|
||||
}
|
||||
|
||||
get host() {
|
||||
return this._getHost();
|
||||
}
|
||||
|
||||
get range() {
|
||||
return this.get(RangeManager);
|
||||
}
|
||||
|
||||
get selection() {
|
||||
return this.get(SelectionManager);
|
||||
}
|
||||
|
||||
get view() {
|
||||
return this.get(ViewStore);
|
||||
}
|
||||
|
||||
constructor(options: BlockStdOptions) {
|
||||
this._getHost = () => {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.ValueNotExists,
|
||||
'Host is not ready to use, the `render` method should be called first'
|
||||
);
|
||||
};
|
||||
this.doc = options.doc;
|
||||
this.userExtensions = options.extensions;
|
||||
this.container = new Container();
|
||||
this.container.addImpl(StdIdentifier, () => this);
|
||||
|
||||
internalExtensions.forEach(ext => {
|
||||
const container = this.container;
|
||||
ext.setup(container);
|
||||
});
|
||||
|
||||
this.userExtensions.forEach(ext => {
|
||||
const container = this.container;
|
||||
ext.setup(container);
|
||||
});
|
||||
|
||||
this.provider = this.container.provider();
|
||||
|
||||
this._lifeCycleWatchers.forEach(watcher => {
|
||||
watcher.created.call(watcher);
|
||||
});
|
||||
}
|
||||
|
||||
getConfig<Key extends BlockSuite.ConfigKeys>(
|
||||
flavour: Key
|
||||
): BlockSuite.BlockConfigs[Key] | null;
|
||||
|
||||
getConfig(flavour: string) {
|
||||
const config = this.provider.getOptional(ConfigIdentifier(flavour));
|
||||
if (!config) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* BlockService will be removed in the future.
|
||||
*/
|
||||
getService<Key extends BlockSuite.ServiceKeys>(
|
||||
flavour: Key
|
||||
): BlockSuite.BlockServices[Key] | null;
|
||||
getService<Service extends BlockService>(flavour: string): Service | null;
|
||||
getService(flavour: string): BlockService | null {
|
||||
return this.getOptional(BlockServiceIdentifier(flavour));
|
||||
}
|
||||
|
||||
getView(flavour: string) {
|
||||
return this.getOptional(BlockViewIdentifier(flavour));
|
||||
}
|
||||
|
||||
mount() {
|
||||
this._lifeCycleWatchers.forEach(watcher => {
|
||||
watcher.mounted.call(watcher);
|
||||
});
|
||||
}
|
||||
|
||||
render() {
|
||||
const element = new EditorHost();
|
||||
element.std = this;
|
||||
element.doc = this.doc;
|
||||
this._getHost = () => element;
|
||||
this._lifeCycleWatchers.forEach(watcher => {
|
||||
watcher.rendered.call(watcher);
|
||||
});
|
||||
|
||||
return element;
|
||||
}
|
||||
|
||||
unmount() {
|
||||
this._lifeCycleWatchers.forEach(watcher => {
|
||||
watcher.unmounted.call(watcher);
|
||||
});
|
||||
this._getHost = () => null as unknown as EditorHost;
|
||||
}
|
||||
}
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface BlockServices {}
|
||||
interface BlockConfigs {}
|
||||
|
||||
type ServiceKeys = string & keyof BlockServices;
|
||||
type ConfigKeys = string & keyof BlockConfigs;
|
||||
|
||||
type Std = BlockStdScope;
|
||||
}
|
||||
}
|
||||
1
blocksuite/framework/block-std/src/scope/index.ts
Normal file
1
blocksuite/framework/block-std/src/scope/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './block-std-scope.js';
|
||||
49
blocksuite/framework/block-std/src/selection/base.ts
Normal file
49
blocksuite/framework/block-std/src/selection/base.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
|
||||
type SelectionConstructor<T = unknown> = {
|
||||
type: string;
|
||||
group: string;
|
||||
new (...args: unknown[]): T;
|
||||
};
|
||||
|
||||
export type BaseSelectionOptions = {
|
||||
blockId: string;
|
||||
};
|
||||
|
||||
export abstract class BaseSelection {
|
||||
static readonly group: string;
|
||||
|
||||
static readonly type: string;
|
||||
|
||||
readonly blockId: string;
|
||||
|
||||
get group(): string {
|
||||
return (this.constructor as SelectionConstructor).group;
|
||||
}
|
||||
|
||||
get type(): BlockSuite.SelectionType {
|
||||
return (this.constructor as SelectionConstructor)
|
||||
.type as BlockSuite.SelectionType;
|
||||
}
|
||||
|
||||
constructor({ blockId }: BaseSelectionOptions) {
|
||||
this.blockId = blockId;
|
||||
}
|
||||
|
||||
static fromJSON(_: Record<string, unknown>): BaseSelection {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.SelectionError,
|
||||
'You must override this method'
|
||||
);
|
||||
}
|
||||
|
||||
abstract equals(other: BaseSelection): boolean;
|
||||
|
||||
is<T extends BlockSuite.SelectionType>(
|
||||
type: T
|
||||
): this is BlockSuite.SelectionInstance[T] {
|
||||
return this.type === type;
|
||||
}
|
||||
|
||||
abstract toJSON(): Record<string, unknown>;
|
||||
}
|
||||
27
blocksuite/framework/block-std/src/selection/index.ts
Normal file
27
blocksuite/framework/block-std/src/selection/index.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import type {
|
||||
BlockSelection,
|
||||
CursorSelection,
|
||||
SurfaceSelection,
|
||||
TextSelection,
|
||||
} from './variants/index.js';
|
||||
|
||||
export * from './base.js';
|
||||
export * from './manager.js';
|
||||
export * from './variants/index.js';
|
||||
|
||||
declare global {
|
||||
namespace BlockSuite {
|
||||
interface Selection {
|
||||
block: typeof BlockSelection;
|
||||
cursor: typeof CursorSelection;
|
||||
surface: typeof SurfaceSelection;
|
||||
text: typeof TextSelection;
|
||||
}
|
||||
|
||||
type SelectionType = keyof Selection;
|
||||
|
||||
type SelectionInstance = {
|
||||
[P in SelectionType]: InstanceType<Selection[P]>;
|
||||
};
|
||||
}
|
||||
}
|
||||
248
blocksuite/framework/block-std/src/selection/manager.ts
Normal file
248
blocksuite/framework/block-std/src/selection/manager.ts
Normal file
@@ -0,0 +1,248 @@
|
||||
import { BlockSuiteError, ErrorCode } from '@blocksuite/global/exceptions';
|
||||
import { DisposableGroup, Slot } from '@blocksuite/global/utils';
|
||||
import { nanoid, type StackItem } from '@blocksuite/store';
|
||||
import { computed, signal } from '@preact/signals-core';
|
||||
|
||||
import { LifeCycleWatcher } from '../extension/index.js';
|
||||
import { SelectionIdentifier } from '../identifier.js';
|
||||
import type { BlockStdScope } from '../scope/index.js';
|
||||
import type { BaseSelection } from './base.js';
|
||||
|
||||
export interface SelectionConstructor {
|
||||
type: string;
|
||||
|
||||
new (...args: any[]): BaseSelection;
|
||||
fromJSON(json: Record<string, unknown>): BaseSelection;
|
||||
}
|
||||
|
||||
export class SelectionManager extends LifeCycleWatcher {
|
||||
static override readonly key = 'selectionManager';
|
||||
|
||||
private _id: string;
|
||||
|
||||
private _itemAdded = (event: { stackItem: StackItem }) => {
|
||||
event.stackItem.meta.set('selection-state', this.value);
|
||||
};
|
||||
|
||||
private _itemPopped = (event: { stackItem: StackItem }) => {
|
||||
const selection = event.stackItem.meta.get('selection-state');
|
||||
if (selection) {
|
||||
this.set(selection as BaseSelection[]);
|
||||
}
|
||||
};
|
||||
|
||||
private _jsonToSelection = (json: Record<string, unknown>) => {
|
||||
const ctor = this._selectionConstructors[json.type as string];
|
||||
if (!ctor) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.SelectionError,
|
||||
`Unknown selection type: ${json.type}`
|
||||
);
|
||||
}
|
||||
return ctor.fromJSON(json);
|
||||
};
|
||||
|
||||
private _remoteSelections = signal<Map<number, BaseSelection[]>>(new Map());
|
||||
|
||||
private _selectionConstructors: Record<string, SelectionConstructor> = {};
|
||||
|
||||
private _selections = signal<BaseSelection[]>([]);
|
||||
|
||||
disposables = new DisposableGroup();
|
||||
|
||||
slots = {
|
||||
changed: new Slot<BaseSelection[]>(),
|
||||
remoteChanged: new Slot<Map<number, BaseSelection[]>>(),
|
||||
};
|
||||
|
||||
private get _store() {
|
||||
return this.std.collection.awarenessStore;
|
||||
}
|
||||
|
||||
get id() {
|
||||
return this._id;
|
||||
}
|
||||
|
||||
get remoteSelections() {
|
||||
return this._remoteSelections.value;
|
||||
}
|
||||
|
||||
get value() {
|
||||
return this._selections.value;
|
||||
}
|
||||
|
||||
constructor(std: BlockStdScope) {
|
||||
super(std);
|
||||
this._id = `${this.std.doc.blockCollection.id}:${nanoid()}`;
|
||||
this._setupDefaultSelections();
|
||||
this._store.awareness.on(
|
||||
'change',
|
||||
(change: { updated: number[]; added: number[]; removed: number[] }) => {
|
||||
const all = change.updated.concat(change.added).concat(change.removed);
|
||||
const localClientID = this._store.awareness.clientID;
|
||||
const exceptLocal = all.filter(id => id !== localClientID);
|
||||
const hasLocal = all.includes(localClientID);
|
||||
if (hasLocal) {
|
||||
const localSelectionJson = this._store.getLocalSelection(this.id);
|
||||
const localSelection = localSelectionJson.map(json => {
|
||||
return this._jsonToSelection(json);
|
||||
});
|
||||
this._selections.value = localSelection;
|
||||
}
|
||||
|
||||
// Only consider remote selections from other clients
|
||||
if (exceptLocal.length > 0) {
|
||||
const map = new Map<number, BaseSelection[]>();
|
||||
this._store.getStates().forEach((state, id) => {
|
||||
if (id === this._store.awareness.clientID) return;
|
||||
// selection id starts with the same block collection id from others clients would be considered as remote selections
|
||||
const selection = Object.entries(state.selectionV2)
|
||||
.filter(([key]) =>
|
||||
key.startsWith(this.std.doc.blockCollection.id)
|
||||
)
|
||||
.flatMap(([_, selection]) => selection);
|
||||
|
||||
const selections = selection
|
||||
.map(json => {
|
||||
try {
|
||||
return this._jsonToSelection(json);
|
||||
} catch (error) {
|
||||
console.error(
|
||||
'Parse remote selection failed:',
|
||||
id,
|
||||
json,
|
||||
error
|
||||
);
|
||||
return null;
|
||||
}
|
||||
})
|
||||
.filter((sel): sel is BaseSelection => !!sel);
|
||||
|
||||
map.set(id, selections);
|
||||
});
|
||||
this._remoteSelections.value = map;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
private _setupDefaultSelections() {
|
||||
this.std.provider.getAll(SelectionIdentifier).forEach(ctor => {
|
||||
this.register(ctor);
|
||||
});
|
||||
}
|
||||
|
||||
clear(types?: string[]) {
|
||||
if (types) {
|
||||
const values = this.value.filter(
|
||||
selection => !types.includes(selection.type)
|
||||
);
|
||||
this.set(values);
|
||||
} else {
|
||||
this.set([]);
|
||||
}
|
||||
}
|
||||
|
||||
create<T extends BlockSuite.SelectionType>(
|
||||
type: T,
|
||||
...args: ConstructorParameters<BlockSuite.Selection[T]>
|
||||
): BlockSuite.SelectionInstance[T] {
|
||||
const ctor = this._selectionConstructors[type];
|
||||
if (!ctor) {
|
||||
throw new BlockSuiteError(
|
||||
ErrorCode.SelectionError,
|
||||
`Unknown selection type: ${type}`
|
||||
);
|
||||
}
|
||||
return new ctor(...args) as BlockSuite.SelectionInstance[T];
|
||||
}
|
||||
|
||||
dispose() {
|
||||
Object.values(this.slots).forEach(slot => slot.dispose());
|
||||
this.disposables.dispose();
|
||||
}
|
||||
|
||||
filter<T extends BlockSuite.SelectionType>(type: T) {
|
||||
return this.filter$(type).value;
|
||||
}
|
||||
|
||||
filter$<T extends BlockSuite.SelectionType>(type: T) {
|
||||
return computed(() =>
|
||||
this.value.filter((sel): sel is BlockSuite.SelectionInstance[T] =>
|
||||
sel.is(type)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
find<T extends BlockSuite.SelectionType>(type: T) {
|
||||
return this.find$(type).value;
|
||||
}
|
||||
|
||||
find$<T extends BlockSuite.SelectionType>(type: T) {
|
||||
return computed(() =>
|
||||
this.value.find((sel): sel is BlockSuite.SelectionInstance[T] =>
|
||||
sel.is(type)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
fromJSON(json: Record<string, unknown>[]) {
|
||||
const selections = json.map(json => {
|
||||
return this._jsonToSelection(json);
|
||||
});
|
||||
return this.set(selections);
|
||||
}
|
||||
|
||||
getGroup(group: string) {
|
||||
return this.value.filter(s => s.group === group);
|
||||
}
|
||||
|
||||
override mounted() {
|
||||
if (this.disposables.disposed) {
|
||||
this.disposables = new DisposableGroup();
|
||||
}
|
||||
this.std.doc.history.on('stack-item-added', this._itemAdded);
|
||||
this.std.doc.history.on('stack-item-popped', this._itemPopped);
|
||||
this.disposables.add(
|
||||
this._store.slots.update.on(({ id }) => {
|
||||
if (id === this._store.awareness.clientID) {
|
||||
return;
|
||||
}
|
||||
this.slots.remoteChanged.emit(this.remoteSelections);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
register(ctor: SelectionConstructor | SelectionConstructor[]) {
|
||||
[ctor].flat().forEach(ctor => {
|
||||
this._selectionConstructors[ctor.type] = ctor;
|
||||
});
|
||||
return this;
|
||||
}
|
||||
|
||||
set(selections: BaseSelection[]) {
|
||||
this._store.setLocalSelection(
|
||||
this.id,
|
||||
selections.map(s => s.toJSON())
|
||||
);
|
||||
this.slots.changed.emit(selections);
|
||||
}
|
||||
|
||||
setGroup(group: string, selections: BaseSelection[]) {
|
||||
const current = this.value.filter(s => s.group !== group);
|
||||
this.set([...current, ...selections]);
|
||||
}
|
||||
|
||||
override unmounted() {
|
||||
this.std.doc.history.off('stack-item-added', this._itemAdded);
|
||||
this.std.doc.history.off('stack-item-popped', this._itemPopped);
|
||||
this.slots.changed.dispose();
|
||||
this.disposables.dispose();
|
||||
this.clear();
|
||||
}
|
||||
|
||||
update(fn: (currentSelections: BaseSelection[]) => BaseSelection[]) {
|
||||
const selections = fn(this.value);
|
||||
this.set(selections);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import z from 'zod';
|
||||
|
||||
import { SelectionExtension } from '../../extension/selection.js';
|
||||
import { BaseSelection } from '../base.js';
|
||||
|
||||
const BlockSelectionSchema = z.object({
|
||||
blockId: z.string(),
|
||||
});
|
||||
|
||||
export class BlockSelection extends BaseSelection {
|
||||
static override group = 'note';
|
||||
|
||||
static override type = 'block';
|
||||
|
||||
static override fromJSON(json: Record<string, unknown>): BlockSelection {
|
||||
const result = BlockSelectionSchema.parse(json);
|
||||
return new BlockSelection(result);
|
||||
}
|
||||
|
||||
override equals(other: BaseSelection): boolean {
|
||||
if (other instanceof BlockSelection) {
|
||||
return this.blockId === other.blockId;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
override toJSON(): Record<string, unknown> {
|
||||
return {
|
||||
type: 'block',
|
||||
blockId: this.blockId,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const BlockSelectionExtension = SelectionExtension(BlockSelection);
|
||||
@@ -0,0 +1,47 @@
|
||||
import z from 'zod';
|
||||
|
||||
import { SelectionExtension } from '../../extension/selection.js';
|
||||
import { BaseSelection } from '../base.js';
|
||||
|
||||
const CursorSelectionSchema = z.object({
|
||||
x: z.number(),
|
||||
y: z.number(),
|
||||
});
|
||||
|
||||
export class CursorSelection extends BaseSelection {
|
||||
static override group = 'gfx';
|
||||
|
||||
static override type = 'cursor';
|
||||
|
||||
readonly x: number;
|
||||
|
||||
readonly y: number;
|
||||
|
||||
constructor(x: number, y: number) {
|
||||
super({ blockId: '[gfx-cursor]' });
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
static override fromJSON(json: Record<string, unknown>): CursorSelection {
|
||||
const { x, y } = CursorSelectionSchema.parse(json);
|
||||
return new CursorSelection(x, y);
|
||||
}
|
||||
|
||||
override equals(other: BaseSelection): boolean {
|
||||
if (other instanceof CursorSelection) {
|
||||
return this.x === other.x && this.y === other.y;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
override toJSON(): Record<string, unknown> {
|
||||
return {
|
||||
type: 'cursor',
|
||||
x: this.x,
|
||||
y: this.y,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const CursorSelectionExtension = SelectionExtension(CursorSelection);
|
||||
@@ -0,0 +1,4 @@
|
||||
export * from './block.js';
|
||||
export * from './cursor.js';
|
||||
export * from './surface.js';
|
||||
export * from './text.js';
|
||||
@@ -0,0 +1,72 @@
|
||||
import z from 'zod';
|
||||
|
||||
import { SelectionExtension } from '../../extension/selection.js';
|
||||
import { BaseSelection } from '../base.js';
|
||||
|
||||
const SurfaceSelectionSchema = z.object({
|
||||
blockId: z.string(),
|
||||
elements: z.array(z.string()),
|
||||
editing: z.boolean(),
|
||||
inoperable: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export class SurfaceSelection extends BaseSelection {
|
||||
static override group = 'gfx';
|
||||
|
||||
static override type = 'surface';
|
||||
|
||||
readonly editing: boolean;
|
||||
|
||||
readonly elements: string[];
|
||||
|
||||
readonly inoperable: boolean;
|
||||
|
||||
constructor(
|
||||
blockId: string,
|
||||
elements: string[],
|
||||
editing: boolean,
|
||||
inoperable = false
|
||||
) {
|
||||
super({ blockId });
|
||||
|
||||
this.elements = elements;
|
||||
this.editing = editing;
|
||||
this.inoperable = inoperable;
|
||||
}
|
||||
|
||||
static override fromJSON(json: Record<string, unknown>): SurfaceSelection {
|
||||
const { blockId, elements, editing, inoperable } =
|
||||
SurfaceSelectionSchema.parse(json);
|
||||
return new SurfaceSelection(blockId, elements, editing, inoperable);
|
||||
}
|
||||
|
||||
override equals(other: BaseSelection): boolean {
|
||||
if (other instanceof SurfaceSelection) {
|
||||
return (
|
||||
this.blockId === other.blockId &&
|
||||
this.editing === other.editing &&
|
||||
this.inoperable === other.inoperable &&
|
||||
this.elements.length === other.elements.length &&
|
||||
this.elements.every((id, idx) => id === other.elements[idx])
|
||||
);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
return this.elements.length === 0 && !this.editing;
|
||||
}
|
||||
|
||||
override toJSON(): Record<string, unknown> {
|
||||
return {
|
||||
type: 'surface',
|
||||
blockId: this.blockId,
|
||||
elements: this.elements,
|
||||
editing: this.editing,
|
||||
inoperable: this.inoperable,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const SurfaceSelectionExtension = SelectionExtension(SurfaceSelection);
|
||||
117
blocksuite/framework/block-std/src/selection/variants/text.ts
Normal file
117
blocksuite/framework/block-std/src/selection/variants/text.ts
Normal file
@@ -0,0 +1,117 @@
|
||||
import z from 'zod';
|
||||
|
||||
import { SelectionExtension } from '../../extension/selection.js';
|
||||
import { BaseSelection } from '../base.js';
|
||||
|
||||
export type TextRangePoint = {
|
||||
blockId: string;
|
||||
index: number;
|
||||
length: number;
|
||||
};
|
||||
|
||||
export type TextSelectionProps = {
|
||||
from: TextRangePoint;
|
||||
to: TextRangePoint | null;
|
||||
reverse?: boolean;
|
||||
};
|
||||
|
||||
const TextSelectionSchema = z.object({
|
||||
from: z.object({
|
||||
blockId: z.string(),
|
||||
index: z.number(),
|
||||
length: z.number(),
|
||||
}),
|
||||
to: z
|
||||
.object({
|
||||
blockId: z.string(),
|
||||
index: z.number(),
|
||||
length: z.number(),
|
||||
})
|
||||
.nullable(),
|
||||
// The `optional()` is for backward compatibility,
|
||||
// since `reverse` may not exist in remote selection.
|
||||
reverse: z.boolean().optional(),
|
||||
});
|
||||
|
||||
export class TextSelection extends BaseSelection {
|
||||
static override group = 'note';
|
||||
|
||||
static override type = 'text';
|
||||
|
||||
from: TextRangePoint;
|
||||
|
||||
reverse: boolean;
|
||||
|
||||
to: TextRangePoint | null;
|
||||
|
||||
get end(): TextRangePoint {
|
||||
return this.reverse ? this.from : (this.to ?? this.from);
|
||||
}
|
||||
|
||||
get start(): TextRangePoint {
|
||||
return this.reverse ? (this.to ?? this.from) : this.from;
|
||||
}
|
||||
|
||||
constructor({ from, to, reverse }: TextSelectionProps) {
|
||||
super({
|
||||
blockId: from.blockId,
|
||||
});
|
||||
this.from = from;
|
||||
|
||||
this.to = this._equalPoint(from, to) ? null : to;
|
||||
|
||||
this.reverse = !!reverse;
|
||||
}
|
||||
|
||||
static override fromJSON(json: Record<string, unknown>): TextSelection {
|
||||
const result = TextSelectionSchema.parse(json);
|
||||
return new TextSelection(result);
|
||||
}
|
||||
|
||||
private _equalPoint(
|
||||
a: TextRangePoint | null,
|
||||
b: TextRangePoint | null
|
||||
): boolean {
|
||||
if (a && b) {
|
||||
return (
|
||||
a.blockId === b.blockId && a.index === b.index && a.length === b.length
|
||||
);
|
||||
}
|
||||
|
||||
return a === b;
|
||||
}
|
||||
|
||||
empty(): boolean {
|
||||
return !!this.to;
|
||||
}
|
||||
|
||||
override equals(other: BaseSelection): boolean {
|
||||
if (other instanceof TextSelection) {
|
||||
return (
|
||||
this.blockId === other.blockId &&
|
||||
this._equalPoint(other.from, this.from) &&
|
||||
this._equalPoint(other.to, this.to)
|
||||
);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
isCollapsed(): boolean {
|
||||
return this.to === null && this.from.length === 0;
|
||||
}
|
||||
|
||||
isInSameBlock(): boolean {
|
||||
return this.to === null || this.from.blockId === this.to.blockId;
|
||||
}
|
||||
|
||||
override toJSON(): Record<string, unknown> {
|
||||
return {
|
||||
type: 'text',
|
||||
from: this.from,
|
||||
to: this.to,
|
||||
reverse: this.reverse,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
export const TextSelectionExtension = SelectionExtension(TextSelection);
|
||||
22
blocksuite/framework/block-std/src/service/index.ts
Normal file
22
blocksuite/framework/block-std/src/service/index.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { LifeCycleWatcher } from '../extension/index.js';
|
||||
import { BlockServiceIdentifier } from '../identifier.js';
|
||||
|
||||
export class ServiceManager extends LifeCycleWatcher {
|
||||
static override readonly key = 'serviceManager';
|
||||
|
||||
override mounted() {
|
||||
super.mounted();
|
||||
|
||||
this.std.provider.getAll(BlockServiceIdentifier).forEach(service => {
|
||||
service.mounted();
|
||||
});
|
||||
}
|
||||
|
||||
override unmounted() {
|
||||
super.unmounted();
|
||||
|
||||
this.std.provider.getAll(BlockServiceIdentifier).forEach(service => {
|
||||
service.unmounted();
|
||||
});
|
||||
}
|
||||
}
|
||||
2
blocksuite/framework/block-std/src/spec/index.ts
Normal file
2
blocksuite/framework/block-std/src/spec/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './slots.js';
|
||||
export * from './type.js';
|
||||
27
blocksuite/framework/block-std/src/spec/slots.ts
Normal file
27
blocksuite/framework/block-std/src/spec/slots.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Slot } from '@blocksuite/global/utils';
|
||||
|
||||
import type { BlockService } from '../extension/service.js';
|
||||
import type { BlockComponent, WidgetComponent } from '../view/index.js';
|
||||
|
||||
export type BlockSpecSlots<Service extends BlockService = BlockService> = {
|
||||
mounted: Slot<{ service: Service }>;
|
||||
unmounted: Slot<{ service: Service }>;
|
||||
viewConnected: Slot<{ component: BlockComponent; service: Service }>;
|
||||
viewDisconnected: Slot<{ component: BlockComponent; service: Service }>;
|
||||
widgetConnected: Slot<{ component: WidgetComponent; service: Service }>;
|
||||
widgetDisconnected: Slot<{
|
||||
component: WidgetComponent;
|
||||
service: Service;
|
||||
}>;
|
||||
};
|
||||
|
||||
export const getSlots = (): BlockSpecSlots => {
|
||||
return {
|
||||
mounted: new Slot(),
|
||||
unmounted: new Slot(),
|
||||
viewConnected: new Slot(),
|
||||
viewDisconnected: new Slot(),
|
||||
widgetConnected: new Slot(),
|
||||
widgetDisconnected: new Slot(),
|
||||
};
|
||||
};
|
||||
6
blocksuite/framework/block-std/src/spec/type.ts
Normal file
6
blocksuite/framework/block-std/src/spec/type.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import type { BlockModel } from '@blocksuite/store';
|
||||
import type { StaticValue } from 'lit/static-html.js';
|
||||
|
||||
export type BlockCommands = Partial<BlockSuite.Commands>;
|
||||
export type BlockViewType = StaticValue | ((model: BlockModel) => StaticValue);
|
||||
export type WidgetViewMapType = Record<string, StaticValue>;
|
||||
@@ -0,0 +1,66 @@
|
||||
import { generateKeyBetween } from 'fractional-indexing';
|
||||
|
||||
function hasSamePrefix(a: string, b: string) {
|
||||
return a.startsWith(b) || b.startsWith(a);
|
||||
}
|
||||
/**
|
||||
* generate a key between a and b, the result key is always satisfied with a < result < b.
|
||||
* the key always has a random suffix, so there is no need to worry about collision.
|
||||
*
|
||||
* make sure a and b are generated by this function.
|
||||
*
|
||||
* @param customPostfix custom postfix for the key, only letters and numbers are allowed
|
||||
*/
|
||||
export function generateKeyBetweenV2(a: string | null, b: string | null) {
|
||||
const randomSize = 32;
|
||||
function postfix(length: number = randomSize) {
|
||||
const chars =
|
||||
'123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
|
||||
const values = new Uint8Array(length);
|
||||
crypto.getRandomValues(values);
|
||||
let result = '';
|
||||
for (let i = 0; i < length; i++) {
|
||||
result += chars.charAt(values[i] % chars.length);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
if (a !== null && b !== null && a >= b) {
|
||||
throw new Error('a should be smaller than b');
|
||||
}
|
||||
// get the subkey in full key
|
||||
// e.g.
|
||||
// a0xxxx -> a
|
||||
// a0x0xxxx -> a0x
|
||||
function subkey(key: string | null) {
|
||||
if (key === null) {
|
||||
return null;
|
||||
}
|
||||
if (key.length <= randomSize + 1) {
|
||||
// no subkey
|
||||
return key;
|
||||
}
|
||||
const splitAt = key.substring(0, key.length - randomSize - 1);
|
||||
return splitAt;
|
||||
}
|
||||
const aSubkey = subkey(a);
|
||||
const bSubkey = subkey(b);
|
||||
if (aSubkey === null && bSubkey === null) {
|
||||
// generate a new key
|
||||
return generateKeyBetween(null, null) + '0' + postfix();
|
||||
} else if (aSubkey === null && bSubkey !== null) {
|
||||
// generate a key before b
|
||||
return generateKeyBetween(null, bSubkey) + '0' + postfix();
|
||||
} else if (bSubkey === null && aSubkey !== null) {
|
||||
// generate a key after a
|
||||
return generateKeyBetween(aSubkey, null) + '0' + postfix();
|
||||
} else if (aSubkey !== null && bSubkey !== null) {
|
||||
// generate a key between a and b
|
||||
if (hasSamePrefix(aSubkey, bSubkey) && a !== null && b !== null) {
|
||||
// conflict, if the subkeys are the same, generate a key between fullkeys
|
||||
return generateKeyBetween(a, b) + '0' + postfix();
|
||||
} else {
|
||||
return generateKeyBetween(aSubkey, bSubkey) + '0' + postfix();
|
||||
}
|
||||
}
|
||||
throw new Error('Never reach here');
|
||||
}
|
||||
32
blocksuite/framework/block-std/src/utils/gfx.ts
Normal file
32
blocksuite/framework/block-std/src/utils/gfx.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import type { Doc } from '@blocksuite/store';
|
||||
import { effect } from '@preact/signals-core';
|
||||
|
||||
import { SurfaceBlockModel } from '../gfx/model/surface/surface-model.js';
|
||||
|
||||
export function onSurfaceAdded(
|
||||
doc: Doc,
|
||||
callback: (model: SurfaceBlockModel | null) => void
|
||||
) {
|
||||
let found = false;
|
||||
let foundId = '';
|
||||
|
||||
const dispose = effect(() => {
|
||||
// if the surface is already found, no need to search again
|
||||
if (found && doc.getBlock(foundId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (const block of Object.values(doc.blocks.value)) {
|
||||
if (block.model instanceof SurfaceBlockModel) {
|
||||
callback(block.model);
|
||||
found = true;
|
||||
foundId = block.id;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
callback(null);
|
||||
});
|
||||
|
||||
return dispose;
|
||||
}
|
||||
1
blocksuite/framework/block-std/src/utils/index.ts
Normal file
1
blocksuite/framework/block-std/src/utils/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export * from './path-finder.js';
|
||||
138
blocksuite/framework/block-std/src/utils/layer.ts
Normal file
138
blocksuite/framework/block-std/src/utils/layer.ts
Normal file
@@ -0,0 +1,138 @@
|
||||
import { nToLast } from '@blocksuite/global/utils';
|
||||
import type { Doc } from '@blocksuite/store';
|
||||
|
||||
import type { GfxLocalElementModel } from '../gfx/index.js';
|
||||
import type { Layer } from '../gfx/layer.js';
|
||||
import {
|
||||
type GfxGroupCompatibleInterface,
|
||||
isGfxGroupCompatibleModel,
|
||||
} from '../gfx/model/base.js';
|
||||
import type { GfxBlockElementModel } from '../gfx/model/gfx-block-model.js';
|
||||
import type { GfxModel } from '../gfx/model/model.js';
|
||||
import type { SurfaceBlockModel } from '../gfx/model/surface/surface-model.js';
|
||||
|
||||
export function getLayerEndZIndex(layers: Layer[], layerIndex: number) {
|
||||
const layer = layers[layerIndex];
|
||||
return layer
|
||||
? layer.type === 'block'
|
||||
? layer.zIndex + layer.elements.length - 1
|
||||
: layer.zIndex
|
||||
: 0;
|
||||
}
|
||||
|
||||
export function updateLayersZIndex(layers: Layer[], startIdx: number) {
|
||||
const startLayer = layers[startIdx];
|
||||
let curIndex = startLayer.zIndex;
|
||||
|
||||
for (let i = startIdx; i < layers.length; ++i) {
|
||||
const curLayer = layers[i];
|
||||
|
||||
curLayer.zIndex = curIndex;
|
||||
curIndex += curLayer.type === 'block' ? curLayer.elements.length : 1;
|
||||
}
|
||||
}
|
||||
|
||||
export function getElementIndex(indexable: GfxModel) {
|
||||
const groups = indexable.groups as GfxGroupCompatibleInterface[];
|
||||
|
||||
if (groups.length) {
|
||||
const groupIndexes = groups
|
||||
.map(group => group.index)
|
||||
.reverse()
|
||||
.join('-');
|
||||
|
||||
return `${groupIndexes}-${indexable.index}`;
|
||||
}
|
||||
|
||||
return indexable.index;
|
||||
}
|
||||
|
||||
export function ungroupIndex(index: string) {
|
||||
return index.split('-')[0];
|
||||
}
|
||||
|
||||
export function insertToOrderedArray(array: GfxModel[], element: GfxModel) {
|
||||
let idx = 0;
|
||||
while (
|
||||
idx < array.length &&
|
||||
[SortOrder.BEFORE, SortOrder.SAME].includes(compare(array[idx], element))
|
||||
) {
|
||||
++idx;
|
||||
}
|
||||
|
||||
array.splice(idx, 0, element);
|
||||
}
|
||||
|
||||
export function removeFromOrderedArray(array: GfxModel[], element: GfxModel) {
|
||||
const idx = array.indexOf(element);
|
||||
|
||||
if (idx !== -1) {
|
||||
array.splice(idx, 1);
|
||||
}
|
||||
}
|
||||
|
||||
export enum SortOrder {
|
||||
AFTER = 1,
|
||||
BEFORE = -1,
|
||||
SAME = 0,
|
||||
}
|
||||
|
||||
export function isInRange(edges: [GfxModel, GfxModel], target: GfxModel) {
|
||||
return compare(target, edges[0]) >= 0 && compare(target, edges[1]) < 0;
|
||||
}
|
||||
|
||||
export function renderableInEdgeless(
|
||||
doc: Doc,
|
||||
surface: SurfaceBlockModel,
|
||||
block: GfxBlockElementModel
|
||||
) {
|
||||
const parent = doc.getParent(block);
|
||||
|
||||
return parent === doc.root || parent === surface;
|
||||
}
|
||||
|
||||
/**
|
||||
* A comparator function for sorting elements in the surface.
|
||||
* SortOrder.AFTER means a should be rendered after b and so on.
|
||||
* @returns
|
||||
*/
|
||||
export function compare(
|
||||
a: GfxModel | GfxLocalElementModel,
|
||||
b: GfxModel | GfxLocalElementModel
|
||||
) {
|
||||
if (isGfxGroupCompatibleModel(a) && b.groups.includes(a)) {
|
||||
return SortOrder.BEFORE;
|
||||
} else if (isGfxGroupCompatibleModel(b) && a.groups.includes(b)) {
|
||||
return SortOrder.AFTER;
|
||||
} else {
|
||||
const aGroups = a.groups as GfxGroupCompatibleInterface[];
|
||||
const bGroups = b.groups as GfxGroupCompatibleInterface[];
|
||||
|
||||
let i = 1;
|
||||
let aGroup:
|
||||
| GfxModel
|
||||
| GfxGroupCompatibleInterface
|
||||
| GfxLocalElementModel
|
||||
| undefined = nToLast(aGroups, i);
|
||||
let bGroup:
|
||||
| GfxModel
|
||||
| GfxGroupCompatibleInterface
|
||||
| GfxLocalElementModel
|
||||
| undefined = nToLast(bGroups, i);
|
||||
|
||||
while (aGroup === bGroup && aGroup) {
|
||||
++i;
|
||||
aGroup = nToLast(aGroups, i);
|
||||
bGroup = nToLast(bGroups, i);
|
||||
}
|
||||
|
||||
aGroup = aGroup ?? a;
|
||||
bGroup = bGroup ?? b;
|
||||
|
||||
return aGroup.index === bGroup.index
|
||||
? SortOrder.SAME
|
||||
: aGroup.index < bGroup.index
|
||||
? SortOrder.BEFORE
|
||||
: SortOrder.AFTER;
|
||||
}
|
||||
}
|
||||
30
blocksuite/framework/block-std/src/utils/path-finder.ts
Normal file
30
blocksuite/framework/block-std/src/utils/path-finder.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export class PathFinder {
|
||||
static equals = (path1: readonly string[], path2: readonly string[]) => {
|
||||
return PathFinder.pathToKey(path1) === PathFinder.pathToKey(path2);
|
||||
};
|
||||
|
||||
static id = (path: readonly string[]) => {
|
||||
return path[path.length - 1];
|
||||
};
|
||||
|
||||
// check if path1 includes path2
|
||||
static includes = (path1: string[], path2: string[]) => {
|
||||
return PathFinder.pathToKey(path1).startsWith(PathFinder.pathToKey(path2));
|
||||
};
|
||||
|
||||
static keyToPath = (key: string) => {
|
||||
return key.split('|');
|
||||
};
|
||||
|
||||
static parent = (path: readonly string[]) => {
|
||||
return path.slice(0, path.length - 1);
|
||||
};
|
||||
|
||||
static pathToKey = (path: readonly string[]) => {
|
||||
return path.join('|');
|
||||
};
|
||||
|
||||
private constructor() {
|
||||
// this is a static class
|
||||
}
|
||||
}
|
||||
137
blocksuite/framework/block-std/src/utils/tree.ts
Normal file
137
blocksuite/framework/block-std/src/utils/tree.ts
Normal file
@@ -0,0 +1,137 @@
|
||||
import type { Doc } from '@blocksuite/store';
|
||||
|
||||
import {
|
||||
type GfxCompatibleInterface,
|
||||
type GfxGroupCompatibleInterface,
|
||||
isGfxGroupCompatibleModel,
|
||||
} from '../gfx/model/base.js';
|
||||
import type { GfxGroupModel, GfxModel } from '../gfx/model/model.js';
|
||||
|
||||
/**
|
||||
* Get the top elements from the list of elements, which are in some tree structures.
|
||||
*
|
||||
* For example: a list `[G1, E1, G2, E2, E3, E4, G4, E5, E6]`,
|
||||
* and they are in the elements tree like:
|
||||
* ```
|
||||
* G1 G4 E6
|
||||
* / \ |
|
||||
* E1 G2 E5
|
||||
* / \
|
||||
* E2 G3*
|
||||
* / \
|
||||
* E3 E4
|
||||
* ```
|
||||
* where the star symbol `*` denote it is not in the list.
|
||||
*
|
||||
* The result should be `[G1, G4, E6]`
|
||||
*/
|
||||
export function getTopElements(elements: GfxModel[]): GfxModel[] {
|
||||
const results = new Set(elements);
|
||||
|
||||
elements = [...new Set(elements)];
|
||||
|
||||
elements.forEach(e1 => {
|
||||
elements.forEach(e2 => {
|
||||
if (isGfxGroupCompatibleModel(e1) && e1.hasDescendant(e2)) {
|
||||
results.delete(e2);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return [...results];
|
||||
}
|
||||
|
||||
function traverse(
|
||||
element: GfxModel,
|
||||
preCallback?: (element: GfxModel) => void | boolean,
|
||||
postCallBack?: (element: GfxModel) => void
|
||||
) {
|
||||
// avoid infinite loop caused by circular reference
|
||||
const visited = new Set<GfxModel>();
|
||||
|
||||
const innerTraverse = (element: GfxModel) => {
|
||||
if (visited.has(element)) return;
|
||||
visited.add(element);
|
||||
|
||||
if (preCallback) {
|
||||
const interrupt = preCallback(element);
|
||||
if (interrupt) return;
|
||||
}
|
||||
|
||||
if (isGfxGroupCompatibleModel(element)) {
|
||||
element.childElements.forEach(child => {
|
||||
innerTraverse(child);
|
||||
});
|
||||
}
|
||||
|
||||
postCallBack && postCallBack(element);
|
||||
};
|
||||
|
||||
innerTraverse(element);
|
||||
}
|
||||
|
||||
export function descendantElementsImpl(
|
||||
container: GfxGroupCompatibleInterface
|
||||
): GfxModel[] {
|
||||
const results: GfxModel[] = [];
|
||||
container.childElements.forEach(child => {
|
||||
traverse(child, element => {
|
||||
results.push(element);
|
||||
});
|
||||
});
|
||||
return results;
|
||||
}
|
||||
|
||||
export function hasDescendantElementImpl(
|
||||
container: GfxGroupCompatibleInterface,
|
||||
element: GfxCompatibleInterface
|
||||
): boolean {
|
||||
let _container = element.group;
|
||||
while (_container) {
|
||||
if (_container === container) return true;
|
||||
_container = _container.group;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* This checker is used to prevent circular reference, when adding a child element to a container.
|
||||
*/
|
||||
export function canSafeAddToContainer(
|
||||
container: GfxGroupModel,
|
||||
element: GfxCompatibleInterface
|
||||
) {
|
||||
if (
|
||||
element === container ||
|
||||
(isGfxGroupCompatibleModel(element) && element.hasDescendant(container))
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
export function isLockedByAncestorImpl(
|
||||
element: GfxCompatibleInterface
|
||||
): boolean {
|
||||
return element.groups.some(isLockedBySelfImpl);
|
||||
}
|
||||
|
||||
export function isLockedBySelfImpl(element: GfxCompatibleInterface): boolean {
|
||||
return element.lockedBySelf ?? false;
|
||||
}
|
||||
|
||||
export function isLockedImpl(element: GfxCompatibleInterface): boolean {
|
||||
return isLockedBySelfImpl(element) || isLockedByAncestorImpl(element);
|
||||
}
|
||||
|
||||
export function lockElementImpl(doc: Doc, element: GfxCompatibleInterface) {
|
||||
doc.transact(() => {
|
||||
element.lockedBySelf = true;
|
||||
});
|
||||
}
|
||||
|
||||
export function unlockElementImpl(doc: Doc, element: GfxCompatibleInterface) {
|
||||
doc.transact(() => {
|
||||
element.lockedBySelf = false;
|
||||
});
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user