-
Notifications
You must be signed in to change notification settings - Fork 79
/
diagram.tsx
116 lines (107 loc) · 3.17 KB
/
diagram.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
Controller,
DocModel,
FocusMode,
IDiagram,
IDiagramProps,
OnChangeFunction
} from '@blink-mind/core';
import '@blink-mind/icons';
import { Hotkey, Hotkeys, HotkeysTarget } from '@blueprintjs/core';
import '@blueprintjs/core/lib/css/blueprint.css';
import '@blueprintjs/select/lib/css/blueprint-select.css';
import debug from 'debug';
import memoizeOne from 'memoize-one';
import * as React from 'react';
import { DefaultPlugin } from '../plugins';
import { HotKeysConfig } from '../types';
import './diagram.scss';
const log = debug('node:Diagram');
// controller 可以为空
interface Props {
docModel: DocModel | null | undefined;
onChange?: OnChangeFunction;
controller?: Controller;
commands?: any;
plugins?: any;
}
@HotkeysTarget
class Diagram extends React.Component<Props> implements IDiagram {
controller: Controller;
public getDiagramProps(): IDiagramProps {
return this.controller.run('getDiagramProps');
}
renderHotkeys() {
const props = this.props;
const controller = props.controller || this.controller;
const model = controller.model;
log('renderHotkeys', model);
const hotKeys: HotKeysConfig = controller.run('customizeHotKeys', {
...props,
controller
});
if (hotKeys === null) return null;
if (
!(
hotKeys.topicHotKeys instanceof Map &&
hotKeys.globalHotKeys instanceof Map
)
) {
throw new TypeError('topicHotKeys and globalHotKeys must be a Map');
}
const children = [];
if (
model.focusMode === FocusMode.NORMAL ||
model.focusMode === FocusMode.SHOW_POPUP ||
model.focusMode === FocusMode.EDITING_CONTENT
) {
hotKeys.topicHotKeys.forEach((v, k) => {
children.push(<Hotkey key={k} {...v} global />);
});
hotKeys.viewModeTopicHotKeys.has(model.config.viewMode) &&
hotKeys.viewModeTopicHotKeys
.get(model.config.viewMode)
.forEach((v, k) => {
children.push(<Hotkey key={k} {...v} global />);
});
}
hotKeys.globalHotKeys.forEach((v, k) => {
children.push(<Hotkey key={k} {...v} global />);
});
return <Hotkeys>{children}</Hotkeys>;
}
public openNewDocModel(newModel: DocModel) {
const props = this.getDiagramProps();
const { controller } = props;
controller.run('openNewDocModel', {
...props,
newModel
});
}
private diagramProps: IDiagramProps;
private resolveController = memoizeOne((plugins = [], TheDefaultPlugin) => {
const defaultPlugin = TheDefaultPlugin();
this.controller = new Controller({
plugins: [plugins, defaultPlugin],
onChange: this.props.onChange
});
});
render() {
const { plugins, controller } = this.props;
if (controller) this.controller = controller;
else this.resolveController(plugins, DefaultPlugin);
let { docModel } = this.props;
if (!docModel) {
docModel = this.controller.run('createNewDocModel', {
controller: this.controller
});
}
this.diagramProps = {
...this.props,
docModel,
controller: this.controller
};
return this.controller.run('renderDiagram', this.diagramProps);
}
}
export { Diagram };