@jupyterlab/attachments 4.0.0-alpha.2 → 4.0.0-alpha.21
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/lib/model.d.ts +2 -12
- package/lib/model.js +3 -27
- package/lib/model.js.map +1 -1
- package/package.json +11 -10
- package/src/index.ts +10 -0
- package/src/model.ts +414 -0
package/lib/model.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as nbformat from '@jupyterlab/nbformat';
|
|
2
|
-
import {
|
|
2
|
+
import { IObservableMap } from '@jupyterlab/observables';
|
|
3
3
|
import { IAttachmentModel } from '@jupyterlab/rendermime';
|
|
4
4
|
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
|
|
5
5
|
import { IDisposable } from '@lumino/disposable';
|
|
@@ -79,10 +79,6 @@ export declare namespace IAttachmentsModel {
|
|
|
79
79
|
* If not given, a default factory will be used.
|
|
80
80
|
*/
|
|
81
81
|
contentFactory?: IContentFactory;
|
|
82
|
-
/**
|
|
83
|
-
* An optional IModelDB to store the attachments model.
|
|
84
|
-
*/
|
|
85
|
-
modelDB?: IModelDB;
|
|
86
82
|
}
|
|
87
83
|
/**
|
|
88
84
|
* A type alias for changed args.
|
|
@@ -105,7 +101,7 @@ export declare class AttachmentsModel implements IAttachmentsModel {
|
|
|
105
101
|
/**
|
|
106
102
|
* Construct a new observable outputs instance.
|
|
107
103
|
*/
|
|
108
|
-
constructor(options
|
|
104
|
+
constructor(options: IAttachmentsModel.IOptions);
|
|
109
105
|
/**
|
|
110
106
|
* A signal emitted when the model state changes.
|
|
111
107
|
*/
|
|
@@ -173,11 +169,6 @@ export declare class AttachmentsModel implements IAttachmentsModel {
|
|
|
173
169
|
* Handle a change to the list.
|
|
174
170
|
*/
|
|
175
171
|
private _onMapChanged;
|
|
176
|
-
/**
|
|
177
|
-
* If the serialized version of the outputs have changed due to a remote
|
|
178
|
-
* action, then update the model accordingly.
|
|
179
|
-
*/
|
|
180
|
-
private _onSerializedChanged;
|
|
181
172
|
/**
|
|
182
173
|
* Handle a change to an item.
|
|
183
174
|
*/
|
|
@@ -186,7 +177,6 @@ export declare class AttachmentsModel implements IAttachmentsModel {
|
|
|
186
177
|
private _isDisposed;
|
|
187
178
|
private _stateChanged;
|
|
188
179
|
private _changed;
|
|
189
|
-
private _modelDB;
|
|
190
180
|
private _serialized;
|
|
191
181
|
private _changeGuard;
|
|
192
182
|
}
|
package/lib/model.js
CHANGED
|
@@ -10,16 +10,16 @@ export class AttachmentsModel {
|
|
|
10
10
|
/**
|
|
11
11
|
* Construct a new observable outputs instance.
|
|
12
12
|
*/
|
|
13
|
-
constructor(options
|
|
13
|
+
constructor(options) {
|
|
14
|
+
var _a;
|
|
14
15
|
this._map = new ObservableMap();
|
|
15
16
|
this._isDisposed = false;
|
|
16
17
|
this._stateChanged = new Signal(this);
|
|
17
18
|
this._changed = new Signal(this);
|
|
18
|
-
this._modelDB = null;
|
|
19
19
|
this._serialized = null;
|
|
20
20
|
this._changeGuard = false;
|
|
21
21
|
this.contentFactory =
|
|
22
|
-
options.contentFactory
|
|
22
|
+
(_a = options.contentFactory) !== null && _a !== void 0 ? _a : AttachmentsModel.defaultContentFactory;
|
|
23
23
|
if (options.values) {
|
|
24
24
|
for (const key of Object.keys(options.values)) {
|
|
25
25
|
if (options.values[key] !== undefined) {
|
|
@@ -28,19 +28,6 @@ export class AttachmentsModel {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
this._map.changed.connect(this._onMapChanged, this);
|
|
31
|
-
// If we are given a IModelDB, keep an up-to-date
|
|
32
|
-
// serialized copy of the AttachmentsModel in it.
|
|
33
|
-
if (options.modelDB) {
|
|
34
|
-
this._modelDB = options.modelDB;
|
|
35
|
-
this._serialized = this._modelDB.createValue('attachments');
|
|
36
|
-
if (this._serialized.get()) {
|
|
37
|
-
this.fromJSON(this._serialized.get());
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
this._serialized.set(this.toJSON());
|
|
41
|
-
}
|
|
42
|
-
this._serialized.changed.connect(this._onSerializedChanged, this);
|
|
43
|
-
}
|
|
44
31
|
}
|
|
45
32
|
/**
|
|
46
33
|
* A signal emitted when the model state changes.
|
|
@@ -163,17 +150,6 @@ export class AttachmentsModel {
|
|
|
163
150
|
this._changed.emit(args);
|
|
164
151
|
this._stateChanged.emit(void 0);
|
|
165
152
|
}
|
|
166
|
-
/**
|
|
167
|
-
* If the serialized version of the outputs have changed due to a remote
|
|
168
|
-
* action, then update the model accordingly.
|
|
169
|
-
*/
|
|
170
|
-
_onSerializedChanged(sender, args) {
|
|
171
|
-
if (!this._changeGuard) {
|
|
172
|
-
this._changeGuard = true;
|
|
173
|
-
this.fromJSON(args.newValue);
|
|
174
|
-
this._changeGuard = false;
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
153
|
/**
|
|
178
154
|
* Handle a change to an item.
|
|
179
155
|
*/
|
package/lib/model.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,2DAA2D;AAG3D,OAAO,
|
|
1
|
+
{"version":3,"file":"model.js","sourceRoot":"","sources":["../src/model.ts"],"names":[],"mappings":"AAAA,0CAA0C;AAC1C,2DAA2D;AAG3D,OAAO,EAGL,aAAa,EACd,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,eAAe,EAEf,oBAAoB,EACrB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EAAW,MAAM,EAAE,MAAM,mBAAmB,CAAC;AA4GpD;;GAEG;AACH,MAAM,OAAO,gBAAgB;IAC3B;;OAEG;IACH,YAAY,OAAmC;;QAoKvC,SAAI,GAAG,IAAI,aAAa,EAAoB,CAAC;QAC7C,gBAAW,GAAG,KAAK,CAAC;QACpB,kBAAa,GAAG,IAAI,MAAM,CAA0B,IAAI,CAAC,CAAC;QAC1D,aAAQ,GAAG,I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EACvD;YACA,MAAM,IAAI,KAAK,CAAC,0CAA0C,QAAQ,IAAI,CAAC,CAAC;SACzE;QACD,MAAM,OAAO,GAAG,QAAQ,QAAQ,WAAW,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;QAC5D,OAAO,OAAO,CAAC;IACjB,CAAC;IAED;;;OAGG;IACH,OAAO,CAAC,GAAW;;QACjB,IAAI,IAAI,CAAC,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,CAAC,aAAa,CAAC,EAAE;YAClD,OAAO,MAAA,MAAA,MAAA,IAAI,CAAC,OAAO,EAAC,OAAO,mDAAG,GAAG,CAAC,mCAAI,IAAI,CAAC;SAC5C;QACD,OAAO,IAAI,CAAC;IACd,CAAC;CAIF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@jupyterlab/attachments",
|
|
3
|
-
"version": "4.0.0-alpha.
|
|
3
|
+
"version": "4.0.0-alpha.21",
|
|
4
4
|
"description": "JupyterLab - Notebook Cell Attachments",
|
|
5
5
|
"homepage": "https://github.com/jupyterlab/jupyterlab",
|
|
6
6
|
"bugs": {
|
|
@@ -27,7 +27,8 @@
|
|
|
27
27
|
"lib/*.js.map",
|
|
28
28
|
"lib/*.js",
|
|
29
29
|
"style/*.css",
|
|
30
|
-
"style/index.js"
|
|
30
|
+
"style/index.js",
|
|
31
|
+
"src/**/*.{ts,tsx}"
|
|
31
32
|
],
|
|
32
33
|
"scripts": {
|
|
33
34
|
"build": "tsc -b",
|
|
@@ -36,17 +37,17 @@
|
|
|
36
37
|
"watch": "tsc -b --watch"
|
|
37
38
|
},
|
|
38
39
|
"dependencies": {
|
|
39
|
-
"@jupyterlab/nbformat": "^4.0.0-alpha.
|
|
40
|
-
"@jupyterlab/observables": "^5.0.0-alpha.
|
|
41
|
-
"@jupyterlab/rendermime": "^4.0.0-alpha.
|
|
42
|
-
"@jupyterlab/rendermime-interfaces": "^
|
|
43
|
-
"@lumino/disposable": "^
|
|
44
|
-
"@lumino/signaling": "^
|
|
40
|
+
"@jupyterlab/nbformat": "^4.0.0-alpha.21",
|
|
41
|
+
"@jupyterlab/observables": "^5.0.0-alpha.21",
|
|
42
|
+
"@jupyterlab/rendermime": "^4.0.0-alpha.21",
|
|
43
|
+
"@jupyterlab/rendermime-interfaces": "^3.8.0-alpha.21",
|
|
44
|
+
"@lumino/disposable": "^2.0.0-rc.1",
|
|
45
|
+
"@lumino/signaling": "^2.0.0-rc.1"
|
|
45
46
|
},
|
|
46
47
|
"devDependencies": {
|
|
47
48
|
"rimraf": "~3.0.0",
|
|
48
|
-
"typedoc": "~0.
|
|
49
|
-
"typescript": "~
|
|
49
|
+
"typedoc": "~0.23.25",
|
|
50
|
+
"typescript": "~5.0.1-rc"
|
|
50
51
|
},
|
|
51
52
|
"publishConfig": {
|
|
52
53
|
"access": "public"
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/* -----------------------------------------------------------------------------
|
|
2
|
+
| Copyright (c) Jupyter Development Team.
|
|
3
|
+
| Distributed under the terms of the Modified BSD License.
|
|
4
|
+
|----------------------------------------------------------------------------*/
|
|
5
|
+
/**
|
|
6
|
+
* @packageDocumentation
|
|
7
|
+
* @module attachments
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export * from './model';
|
package/src/model.ts
ADDED
|
@@ -0,0 +1,414 @@
|
|
|
1
|
+
// Copyright (c) Jupyter Development Team.
|
|
2
|
+
// Distributed under the terms of the Modified BSD License.
|
|
3
|
+
|
|
4
|
+
import * as nbformat from '@jupyterlab/nbformat';
|
|
5
|
+
import {
|
|
6
|
+
IObservableMap,
|
|
7
|
+
IObservableValue,
|
|
8
|
+
ObservableMap
|
|
9
|
+
} from '@jupyterlab/observables';
|
|
10
|
+
import {
|
|
11
|
+
AttachmentModel,
|
|
12
|
+
IAttachmentModel,
|
|
13
|
+
imageRendererFactory
|
|
14
|
+
} from '@jupyterlab/rendermime';
|
|
15
|
+
import { IRenderMime } from '@jupyterlab/rendermime-interfaces';
|
|
16
|
+
import { IDisposable } from '@lumino/disposable';
|
|
17
|
+
import { ISignal, Signal } from '@lumino/signaling';
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The model for attachments.
|
|
21
|
+
*/
|
|
22
|
+
export interface IAttachmentsModel extends IDisposable {
|
|
23
|
+
/**
|
|
24
|
+
* A signal emitted when the model state changes.
|
|
25
|
+
*/
|
|
26
|
+
readonly stateChanged: ISignal<IAttachmentsModel, void>;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A signal emitted when the model changes.
|
|
30
|
+
*/
|
|
31
|
+
readonly changed: ISignal<IAttachmentsModel, IAttachmentsModel.ChangedArgs>;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The length of the items in the model.
|
|
35
|
+
*/
|
|
36
|
+
readonly length: number;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The keys of the attachments in the model.
|
|
40
|
+
*/
|
|
41
|
+
readonly keys: ReadonlyArray<string>;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* The attachment content factory used by the model.
|
|
45
|
+
*/
|
|
46
|
+
readonly contentFactory: IAttachmentsModel.IContentFactory;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Whether the specified key is set.
|
|
50
|
+
*/
|
|
51
|
+
has(key: string): boolean;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Get an item for the specified key.
|
|
55
|
+
*/
|
|
56
|
+
get(key: string): IAttachmentModel | undefined;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Set the value of the specified key.
|
|
60
|
+
*/
|
|
61
|
+
set(key: string, attachment: nbformat.IMimeBundle): void;
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Remove the attachment whose name is the specified key.
|
|
65
|
+
* Note that this is optional only until Jupyterlab 2.0 release.
|
|
66
|
+
*/
|
|
67
|
+
remove: (key: string) => void;
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Clear all of the attachments.
|
|
71
|
+
*/
|
|
72
|
+
clear(): void;
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Deserialize the model from JSON.
|
|
76
|
+
*
|
|
77
|
+
* #### Notes
|
|
78
|
+
* This will clear any existing data.
|
|
79
|
+
*/
|
|
80
|
+
fromJSON(values: nbformat.IAttachments): void;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Serialize the model to JSON.
|
|
84
|
+
*/
|
|
85
|
+
toJSON(): nbformat.IAttachments;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* The namespace for IAttachmentsModel interfaces.
|
|
90
|
+
*/
|
|
91
|
+
export namespace IAttachmentsModel {
|
|
92
|
+
/**
|
|
93
|
+
* The options used to create a attachments model.
|
|
94
|
+
*/
|
|
95
|
+
export interface IOptions {
|
|
96
|
+
/**
|
|
97
|
+
* The initial values for the model.
|
|
98
|
+
*/
|
|
99
|
+
values?: nbformat.IAttachments;
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* The attachment content factory used by the model.
|
|
103
|
+
*
|
|
104
|
+
* If not given, a default factory will be used.
|
|
105
|
+
*/
|
|
106
|
+
contentFactory?: IContentFactory;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* A type alias for changed args.
|
|
111
|
+
*/
|
|
112
|
+
export type ChangedArgs = IObservableMap.IChangedArgs<IAttachmentModel>;
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* The interface for an attachment content factory.
|
|
116
|
+
*/
|
|
117
|
+
export interface IContentFactory {
|
|
118
|
+
/**
|
|
119
|
+
* Create an attachment model.
|
|
120
|
+
*/
|
|
121
|
+
createAttachmentModel(options: IAttachmentModel.IOptions): IAttachmentModel;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The default implementation of the IAttachmentsModel.
|
|
127
|
+
*/
|
|
128
|
+
export class AttachmentsModel implements IAttachmentsModel {
|
|
129
|
+
/**
|
|
130
|
+
* Construct a new observable outputs instance.
|
|
131
|
+
*/
|
|
132
|
+
constructor(options: IAttachmentsModel.IOptions) {
|
|
133
|
+
this.contentFactory =
|
|
134
|
+
options.contentFactory ?? AttachmentsModel.defaultContentFactory;
|
|
135
|
+
if (options.values) {
|
|
136
|
+
for (const key of Object.keys(options.values)) {
|
|
137
|
+
if (options.values[key] !== undefined) {
|
|
138
|
+
this.set(key, options.values[key]!);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
this._map.changed.connect(this._onMapChanged, this);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* A signal emitted when the model state changes.
|
|
147
|
+
*/
|
|
148
|
+
get stateChanged(): ISignal<IAttachmentsModel, void> {
|
|
149
|
+
return this._stateChanged;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* A signal emitted when the model changes.
|
|
154
|
+
*/
|
|
155
|
+
get changed(): ISignal<this, IAttachmentsModel.ChangedArgs> {
|
|
156
|
+
return this._changed;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* The keys of the attachments in the model.
|
|
161
|
+
*/
|
|
162
|
+
get keys(): ReadonlyArray<string> {
|
|
163
|
+
return this._map.keys();
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Get the length of the items in the model.
|
|
168
|
+
*/
|
|
169
|
+
get length(): number {
|
|
170
|
+
return this._map.keys().length;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* The attachment content factory used by the model.
|
|
175
|
+
*/
|
|
176
|
+
readonly contentFactory: IAttachmentsModel.IContentFactory;
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Test whether the model is disposed.
|
|
180
|
+
*/
|
|
181
|
+
get isDisposed(): boolean {
|
|
182
|
+
return this._isDisposed;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Dispose of the resources used by the model.
|
|
187
|
+
*/
|
|
188
|
+
dispose(): void {
|
|
189
|
+
if (this.isDisposed) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
this._isDisposed = true;
|
|
193
|
+
this._map.dispose();
|
|
194
|
+
Signal.clearData(this);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Whether the specified key is set.
|
|
199
|
+
*/
|
|
200
|
+
has(key: string): boolean {
|
|
201
|
+
return this._map.has(key);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Get an item at the specified key.
|
|
206
|
+
*/
|
|
207
|
+
get(key: string): IAttachmentModel | undefined {
|
|
208
|
+
return this._map.get(key);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Set the value at the specified key.
|
|
213
|
+
*/
|
|
214
|
+
set(key: string, value: nbformat.IMimeBundle): void {
|
|
215
|
+
// Normalize stream data.
|
|
216
|
+
const item = this._createItem({ value });
|
|
217
|
+
this._map.set(key, item);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Remove the attachment whose name is the specified key
|
|
222
|
+
*/
|
|
223
|
+
remove(key: string): void {
|
|
224
|
+
this._map.delete(key);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Clear all of the attachments.
|
|
229
|
+
*/
|
|
230
|
+
clear(): void {
|
|
231
|
+
this._map.values().forEach((item: IAttachmentModel) => {
|
|
232
|
+
item.dispose();
|
|
233
|
+
});
|
|
234
|
+
this._map.clear();
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Deserialize the model from JSON.
|
|
239
|
+
*
|
|
240
|
+
* #### Notes
|
|
241
|
+
* This will clear any existing data.
|
|
242
|
+
*/
|
|
243
|
+
fromJSON(values: nbformat.IAttachments): void {
|
|
244
|
+
this.clear();
|
|
245
|
+
Object.keys(values).forEach(key => {
|
|
246
|
+
if (values[key] !== undefined) {
|
|
247
|
+
this.set(key, values[key]!);
|
|
248
|
+
}
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Serialize the model to JSON.
|
|
254
|
+
*/
|
|
255
|
+
toJSON(): nbformat.IAttachments {
|
|
256
|
+
const ret: nbformat.IAttachments = {};
|
|
257
|
+
for (const key of this._map.keys()) {
|
|
258
|
+
ret[key] = this._map.get(key)!.toJSON();
|
|
259
|
+
}
|
|
260
|
+
return ret;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Create an attachment item and hook up its signals.
|
|
265
|
+
*/
|
|
266
|
+
private _createItem(options: IAttachmentModel.IOptions): IAttachmentModel {
|
|
267
|
+
const factory = this.contentFactory;
|
|
268
|
+
const item = factory.createAttachmentModel(options);
|
|
269
|
+
item.changed.connect(this._onGenericChange, this);
|
|
270
|
+
return item;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Handle a change to the list.
|
|
275
|
+
*/
|
|
276
|
+
private _onMapChanged(
|
|
277
|
+
sender: IObservableMap<IAttachmentModel>,
|
|
278
|
+
args: IObservableMap.IChangedArgs<IAttachmentModel>
|
|
279
|
+
) {
|
|
280
|
+
if (this._serialized && !this._changeGuard) {
|
|
281
|
+
this._changeGuard = true;
|
|
282
|
+
this._serialized.set(this.toJSON());
|
|
283
|
+
this._changeGuard = false;
|
|
284
|
+
}
|
|
285
|
+
this._changed.emit(args);
|
|
286
|
+
this._stateChanged.emit(void 0);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Handle a change to an item.
|
|
291
|
+
*/
|
|
292
|
+
private _onGenericChange(): void {
|
|
293
|
+
this._stateChanged.emit(void 0);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
private _map = new ObservableMap<IAttachmentModel>();
|
|
297
|
+
private _isDisposed = false;
|
|
298
|
+
private _stateChanged = new Signal<IAttachmentsModel, void>(this);
|
|
299
|
+
private _changed = new Signal<this, IAttachmentsModel.ChangedArgs>(this);
|
|
300
|
+
private _serialized: IObservableValue | null = null;
|
|
301
|
+
private _changeGuard = false;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* The namespace for AttachmentsModel class statics.
|
|
306
|
+
*/
|
|
307
|
+
export namespace AttachmentsModel {
|
|
308
|
+
/**
|
|
309
|
+
* The default implementation of a `IAttachmentsModel.IContentFactory`.
|
|
310
|
+
*/
|
|
311
|
+
export class ContentFactory implements IAttachmentsModel.IContentFactory {
|
|
312
|
+
/**
|
|
313
|
+
* Create an attachment model.
|
|
314
|
+
*/
|
|
315
|
+
createAttachmentModel(
|
|
316
|
+
options: IAttachmentModel.IOptions
|
|
317
|
+
): IAttachmentModel {
|
|
318
|
+
return new AttachmentModel(options);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* The default attachment model factory.
|
|
324
|
+
*/
|
|
325
|
+
export const defaultContentFactory = new ContentFactory();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* A resolver for cell attachments 'attachment:filename'.
|
|
330
|
+
*
|
|
331
|
+
* Will resolve to a data: url.
|
|
332
|
+
*/
|
|
333
|
+
export class AttachmentsResolver implements IRenderMime.IResolver {
|
|
334
|
+
/**
|
|
335
|
+
* Create an attachments resolver object.
|
|
336
|
+
*/
|
|
337
|
+
constructor(options: AttachmentsResolver.IOptions) {
|
|
338
|
+
this._parent = options.parent || null;
|
|
339
|
+
this._model = options.model;
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Resolve a relative url to a correct server path.
|
|
343
|
+
*/
|
|
344
|
+
async resolveUrl(url: string): Promise<string> {
|
|
345
|
+
if (this._parent && !url.startsWith('attachment:')) {
|
|
346
|
+
return this._parent.resolveUrl(url);
|
|
347
|
+
}
|
|
348
|
+
return url;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/**
|
|
352
|
+
* Get the download url of a given absolute server path.
|
|
353
|
+
*
|
|
354
|
+
* #### Notes
|
|
355
|
+
* The returned URL may include a query parameter.
|
|
356
|
+
*/
|
|
357
|
+
async getDownloadUrl(path: string): Promise<string> {
|
|
358
|
+
if (this._parent && !path.startsWith('attachment:')) {
|
|
359
|
+
return this._parent.getDownloadUrl(path);
|
|
360
|
+
}
|
|
361
|
+
// Return a data URL with the data of the url
|
|
362
|
+
const key = path.slice('attachment:'.length);
|
|
363
|
+
const attachment = this._model.get(key);
|
|
364
|
+
if (attachment === undefined) {
|
|
365
|
+
// Resolve with unprocessed path, to show as broken image
|
|
366
|
+
return path;
|
|
367
|
+
}
|
|
368
|
+
const { data } = attachment;
|
|
369
|
+
const mimeType = Object.keys(data)[0];
|
|
370
|
+
// Only support known safe types:
|
|
371
|
+
if (
|
|
372
|
+
mimeType === undefined ||
|
|
373
|
+
imageRendererFactory.mimeTypes.indexOf(mimeType) === -1
|
|
374
|
+
) {
|
|
375
|
+
throw new Error(`Cannot render unknown image mime type "${mimeType}".`);
|
|
376
|
+
}
|
|
377
|
+
const dataUrl = `data:${mimeType};base64,${data[mimeType]}`;
|
|
378
|
+
return dataUrl;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* Whether the URL should be handled by the resolver
|
|
383
|
+
* or not.
|
|
384
|
+
*/
|
|
385
|
+
isLocal(url: string): boolean {
|
|
386
|
+
if (this._parent && !url.startsWith('attachment:')) {
|
|
387
|
+
return this._parent.isLocal?.(url) ?? true;
|
|
388
|
+
}
|
|
389
|
+
return true;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private _model: IAttachmentsModel;
|
|
393
|
+
private _parent: IRenderMime.IResolver | null;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* The namespace for `AttachmentsResolver` class statics.
|
|
398
|
+
*/
|
|
399
|
+
export namespace AttachmentsResolver {
|
|
400
|
+
/**
|
|
401
|
+
* The options used to create an AttachmentsResolver.
|
|
402
|
+
*/
|
|
403
|
+
export interface IOptions {
|
|
404
|
+
/**
|
|
405
|
+
* The attachments model to resolve against.
|
|
406
|
+
*/
|
|
407
|
+
model: IAttachmentsModel;
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* A parent resolver to use if the URL/path is not for an attachment.
|
|
411
|
+
*/
|
|
412
|
+
parent?: IRenderMime.IResolver;
|
|
413
|
+
}
|
|
414
|
+
}
|