@pixerate/editor-svelte 0.2.4 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +56 -0
- package/dist/index.js +494 -11
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/BubbleMenu.svelte +29 -16
- package/src/index.d.ts +56 -0
- package/src/index.ts +2 -0
- package/src/spreadsheet/FormulaBar.svelte +58 -0
- package/src/spreadsheet/SpreadsheetEditor.svelte +271 -0
- package/src/spreadsheet/index.ts +3 -0
- package/src/spreadsheet/spreadsheet.svelte.ts +159 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SpreadsheetController,
|
|
3
|
+
type SpreadsheetControllerOptions,
|
|
4
|
+
type SpreadsheetDocument,
|
|
5
|
+
type CellCoordinate,
|
|
6
|
+
type CellRange,
|
|
7
|
+
type CellData,
|
|
8
|
+
type SpreadsheetColumn,
|
|
9
|
+
type SpreadsheetRow,
|
|
10
|
+
type SpreadsheetDataSource
|
|
11
|
+
} from '@pixerate/editor';
|
|
12
|
+
|
|
13
|
+
export function createReactiveSpreadsheet(options: SpreadsheetControllerOptions = {}) {
|
|
14
|
+
let doc = $state<SpreadsheetDocument>({
|
|
15
|
+
id: options.document?.id || `sheet_${Date.now()}`,
|
|
16
|
+
name: options.document?.name || 'Sheet 1',
|
|
17
|
+
columns: options.document?.columns || [],
|
|
18
|
+
rows: options.document?.rows || [],
|
|
19
|
+
cells: options.document?.cells || {},
|
|
20
|
+
createdAt: options.document?.createdAt || Date.now(),
|
|
21
|
+
updatedAt: options.document?.updatedAt || Date.now()
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
let activeCell = $state<CellCoordinate | null>(null);
|
|
25
|
+
let selectedRange = $state<CellRange | null>(null);
|
|
26
|
+
let editingCell = $state<CellCoordinate | null>(null);
|
|
27
|
+
let draftValue = $state<string>('');
|
|
28
|
+
|
|
29
|
+
const controller = new SpreadsheetController({
|
|
30
|
+
...options,
|
|
31
|
+
document: options.document,
|
|
32
|
+
onDocumentChange: (updated) => {
|
|
33
|
+
doc = { ...updated };
|
|
34
|
+
options.onDocumentChange?.(updated);
|
|
35
|
+
},
|
|
36
|
+
onSelectionChange: (active, range) => {
|
|
37
|
+
activeCell = active;
|
|
38
|
+
selectedRange = range;
|
|
39
|
+
options.onSelectionChange?.(active, range);
|
|
40
|
+
},
|
|
41
|
+
onCellCommit: (rowId, colId, raw, value) => {
|
|
42
|
+
options.onCellCommit?.(rowId, colId, raw, value);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Keep initial states aligned
|
|
47
|
+
doc = { ...controller.document };
|
|
48
|
+
activeCell = controller.activeCell;
|
|
49
|
+
selectedRange = controller.selectedRange;
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
get document() {
|
|
53
|
+
return doc;
|
|
54
|
+
},
|
|
55
|
+
get activeCell() {
|
|
56
|
+
return activeCell;
|
|
57
|
+
},
|
|
58
|
+
get selectedRange() {
|
|
59
|
+
return selectedRange;
|
|
60
|
+
},
|
|
61
|
+
get editingCell() {
|
|
62
|
+
return editingCell;
|
|
63
|
+
},
|
|
64
|
+
get draftValue() {
|
|
65
|
+
return draftValue;
|
|
66
|
+
},
|
|
67
|
+
set draftValue(val: string) {
|
|
68
|
+
draftValue = val;
|
|
69
|
+
controller.draftValue = val;
|
|
70
|
+
},
|
|
71
|
+
controller,
|
|
72
|
+
|
|
73
|
+
getCell(rowId: string, colId: string): CellData {
|
|
74
|
+
return controller.getCell(rowId, colId);
|
|
75
|
+
},
|
|
76
|
+
setCellValue(rowId: string, colId: string, raw: string) {
|
|
77
|
+
controller.setCellValue(rowId, colId, raw);
|
|
78
|
+
doc = { ...controller.document };
|
|
79
|
+
},
|
|
80
|
+
selectCell(row: number, col: number, extendSelection = false) {
|
|
81
|
+
controller.selectCell(row, col, extendSelection);
|
|
82
|
+
activeCell = controller.activeCell;
|
|
83
|
+
selectedRange = controller.selectedRange;
|
|
84
|
+
},
|
|
85
|
+
selectRange(range: CellRange) {
|
|
86
|
+
controller.selectRange(range);
|
|
87
|
+
selectedRange = controller.selectedRange;
|
|
88
|
+
},
|
|
89
|
+
startEditing(row?: number, col?: number, initialVal?: string) {
|
|
90
|
+
controller.startEditing(row, col, initialVal);
|
|
91
|
+
activeCell = controller.activeCell;
|
|
92
|
+
editingCell = controller.editingCell;
|
|
93
|
+
draftValue = controller.draftValue;
|
|
94
|
+
},
|
|
95
|
+
commitEditing(val?: string) {
|
|
96
|
+
controller.commitEditing(val);
|
|
97
|
+
editingCell = null;
|
|
98
|
+
draftValue = '';
|
|
99
|
+
doc = { ...controller.document };
|
|
100
|
+
},
|
|
101
|
+
cancelEditing() {
|
|
102
|
+
controller.cancelEditing();
|
|
103
|
+
editingCell = null;
|
|
104
|
+
draftValue = '';
|
|
105
|
+
},
|
|
106
|
+
moveCursor(dir: 'up' | 'down' | 'left' | 'right', extend = false) {
|
|
107
|
+
controller.moveCursor(dir, extend);
|
|
108
|
+
activeCell = controller.activeCell;
|
|
109
|
+
selectedRange = controller.selectedRange;
|
|
110
|
+
},
|
|
111
|
+
insertColumn(index: number, config?: Partial<SpreadsheetColumn>) {
|
|
112
|
+
controller.insertColumn(index, config);
|
|
113
|
+
doc = { ...controller.document };
|
|
114
|
+
},
|
|
115
|
+
deleteColumn(colId: string) {
|
|
116
|
+
controller.deleteColumn(colId);
|
|
117
|
+
doc = { ...controller.document };
|
|
118
|
+
},
|
|
119
|
+
insertRow(index: number, config?: Partial<SpreadsheetRow>) {
|
|
120
|
+
controller.insertRow(index, config);
|
|
121
|
+
doc = { ...controller.document };
|
|
122
|
+
},
|
|
123
|
+
deleteRow(rowId: string) {
|
|
124
|
+
controller.deleteRow(rowId);
|
|
125
|
+
doc = { ...controller.document };
|
|
126
|
+
},
|
|
127
|
+
setColumnWidth(colId: string, width: number) {
|
|
128
|
+
controller.setColumnWidth(colId, width);
|
|
129
|
+
doc = { ...controller.document };
|
|
130
|
+
},
|
|
131
|
+
registerDataSource(ds: SpreadsheetDataSource) {
|
|
132
|
+
controller.registerDataSource(ds);
|
|
133
|
+
doc = { ...controller.document };
|
|
134
|
+
},
|
|
135
|
+
setPrimaryDataSource(id: string | undefined) {
|
|
136
|
+
controller.setPrimaryDataSource(id);
|
|
137
|
+
doc = { ...controller.document };
|
|
138
|
+
},
|
|
139
|
+
recalculateAll() {
|
|
140
|
+
controller.recalculateAll();
|
|
141
|
+
doc = { ...controller.document };
|
|
142
|
+
},
|
|
143
|
+
exportToTsv() {
|
|
144
|
+
return controller.exportToTsv();
|
|
145
|
+
},
|
|
146
|
+
importFromTsv(data: string) {
|
|
147
|
+
controller.importFromTsv(data);
|
|
148
|
+
doc = { ...controller.document };
|
|
149
|
+
},
|
|
150
|
+
undo() {
|
|
151
|
+
controller.undo();
|
|
152
|
+
doc = { ...controller.document };
|
|
153
|
+
},
|
|
154
|
+
redo() {
|
|
155
|
+
controller.redo();
|
|
156
|
+
doc = { ...controller.document };
|
|
157
|
+
}
|
|
158
|
+
};
|
|
159
|
+
}
|