-
-
Notifications
You must be signed in to change notification settings - Fork 232
/
table-plugins.ts
162 lines (137 loc) Β· 4.88 KB
/
table-plugins.ts
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { css } from '@emotion/css';
import {
EditorState,
findParentNodeOfType,
FindProsemirrorNodeResult,
ProsemirrorPlugin,
} from '@remirror/core';
import { Plugin, PluginKey, Transaction } from '@remirror/pm/state';
import { Decoration, DecorationSet } from '@remirror/pm/view';
import { ExtensionTablesTheme, getThemeVar } from '@remirror/theme';
import { InsertButtonAttrs } from './components/table-insert-button';
const preselectBorderColor = getThemeVar('color', 'table', 'preselect', 'border');
const preselectControllerBackgroundColor = getThemeVar('color', 'table', 'preselect', 'controller');
export function getTableStyle(attrs: ControllerStateValues): string {
const preselectClass = css`
/* Make the border-style 'double' instead of 'solid'. This works because 'double' has a higher priority than 'solid' */
border-style: double;
border-color: ${preselectBorderColor};
`;
const preselectControllerClass = css`
${preselectClass}
background-color: ${preselectControllerBackgroundColor};
`;
let classNames = '';
if (attrs.preselectColumn !== -1) {
classNames = css`
& table.${ExtensionTablesTheme.TABLE} tbody tr {
td:nth-child(${attrs.preselectColumn + 1}) {
${preselectClass};
}
th.${ExtensionTablesTheme.TABLE_CONTROLLER}:nth-child(${attrs.preselectColumn + 1}) {
${preselectControllerClass}
}
}
`;
} else if (attrs.preselectRow !== -1) {
classNames = css`
& table.${ExtensionTablesTheme.TABLE} tbody tr:nth-child(${attrs.preselectRow + 1}) {
td,
th {
${preselectClass};
}
th.${ExtensionTablesTheme.TABLE_CONTROLLER} {
${preselectControllerClass}
}
}
`;
} else if (attrs.preselectTable) {
classNames = css`
& table.${ExtensionTablesTheme.TABLE} tbody tr {
td,
th {
${preselectClass};
}
th.${ExtensionTablesTheme.TABLE_CONTROLLER}.${ExtensionTablesTheme.TABLE_CONTROLLER} {
${preselectControllerClass}
}
}
`;
}
return classNames;
}
const key = new PluginKey<ControllerState>('remirrorTableControllerPluginKey');
export { key as tableControllerPluginKey };
export function createTableControllerPlugin(): ProsemirrorPlugin<ControllerState> {
return new Plugin<ControllerState>({
key: key,
state: {
init() {
return new ControllerState({});
},
apply(tr, prev: ControllerState) {
return prev.apply(tr);
},
},
props: {
decorations: (state: EditorState) => {
const controllerState = key.getState(state);
if (!controllerState) {
return null;
}
const { tableNodeResult, predelete } = controllerState.values;
if (tableNodeResult) {
const styleClassName = getTableStyle(controllerState.values);
let className = `${ExtensionTablesTheme.TABLE_SHOW_CONTROLLERS} ${styleClassName}`;
if (predelete) {
className += ` ${ExtensionTablesTheme.TABLE_SHOW_PREDELETE}`;
}
const decorations = [
Decoration.node(tableNodeResult.pos, tableNodeResult.end, {
class: className,
}),
];
return DecorationSet.create(state.doc, decorations);
}
return null;
},
},
});
}
interface ControllerStateValues {
tableNodeResult: FindProsemirrorNodeResult | null | undefined; // the table that contains current selection.
preselectTable: boolean; // if this value is true, all table cells will have a "preselect" style.
preselectColumn: number; // if this value is not -1, all cells in this table column will have a "preselect" style.
preselectRow: number; // if this value is not -1, all cells in this table row will have a "preselect" style.
predelete: boolean; // if this value is true, all selected cells will have a "predelete" style.
insertButtonAttrs: InsertButtonAttrs | null; // if and only if `insertButtonAttrs` exists, InsertButton will show.
}
type ControllerStateProps = Omit<Partial<ControllerStateValues>, 'tableNodeResult'>;
class ControllerState {
public values: ControllerStateValues;
constructor(public action: ControllerStateProps) {
this.values = {
tableNodeResult: null,
preselectTable: false,
preselectColumn: -1,
preselectRow: -1,
predelete: false,
insertButtonAttrs: null,
...action,
};
}
apply(tr: Transaction): ControllerState {
this.values.tableNodeResult = findParentNodeOfType({
types: 'table',
selection: tr.selection,
});
const props: ControllerStateProps | null = tr.getMeta(key);
if (props) {
return new ControllerState({ ...this.values, ...props });
}
return this;
}
}
export function setControllerPluginMeta(tr: Transaction, props: ControllerStateProps): Transaction {
return tr.setMeta(key, props);
}