@dxos/plugin-wnfs 0.7.5-main.9d26e3a → 0.7.5-main.9d2a38b
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/lib/browser/FileContainer-I2P6I7CX.mjs +94 -0
- package/dist/lib/browser/FileContainer-I2P6I7CX.mjs.map +7 -0
- package/dist/lib/browser/blockstore-6U5XTSIH.mjs +192 -0
- package/dist/lib/browser/blockstore-6U5XTSIH.mjs.map +7 -0
- package/dist/lib/browser/chunk-357BWHML.mjs +16 -0
- package/dist/lib/browser/chunk-357BWHML.mjs.map +7 -0
- package/dist/lib/browser/{chunk-VL3H7P4F.mjs → chunk-6YFT42F7.mjs} +5 -8
- package/dist/lib/browser/chunk-6YFT42F7.mjs.map +7 -0
- package/dist/lib/browser/chunk-EMRQJXBW.mjs +60 -0
- package/dist/lib/browser/chunk-EMRQJXBW.mjs.map +7 -0
- package/dist/lib/browser/chunk-SCQWJNHD.mjs +157 -0
- package/dist/lib/browser/chunk-SCQWJNHD.mjs.map +7 -0
- package/dist/lib/browser/chunk-YLGCDZV3.mjs +16 -0
- package/dist/lib/browser/chunk-YLGCDZV3.mjs.map +7 -0
- package/dist/lib/browser/file-uploader-EDHMSKYS.mjs +29 -0
- package/dist/lib/browser/file-uploader-EDHMSKYS.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +96 -486
- package/dist/lib/browser/index.mjs.map +4 -4
- package/dist/lib/browser/intent-resolver-WDO4EIG3.mjs +43 -0
- package/dist/lib/browser/intent-resolver-WDO4EIG3.mjs.map +7 -0
- package/dist/lib/browser/markdown-MGSH5CVQ.mjs +198 -0
- package/dist/lib/browser/markdown-MGSH5CVQ.mjs.map +7 -0
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/react-surface-EBQICOUA.mjs +81 -0
- package/dist/lib/browser/react-surface-EBQICOUA.mjs.map +7 -0
- package/dist/lib/browser/types/index.mjs +2 -2
- package/dist/types/src/WnfsPlugin.d.ts +1 -3
- package/dist/types/src/WnfsPlugin.d.ts.map +1 -1
- package/dist/types/src/blockstore.d.ts +4 -1
- package/dist/types/src/blockstore.d.ts.map +1 -1
- package/dist/types/src/capabilities/blockstore.d.ts +4 -0
- package/dist/types/src/capabilities/blockstore.d.ts.map +1 -0
- package/dist/types/src/capabilities/capabilities.d.ts +12 -0
- package/dist/types/src/capabilities/capabilities.d.ts.map +1 -0
- package/dist/types/src/capabilities/file-uploader.d.ts +4 -0
- package/dist/types/src/capabilities/file-uploader.d.ts.map +1 -0
- package/dist/types/src/capabilities/index.d.ts +7 -0
- package/dist/types/src/capabilities/index.d.ts.map +1 -0
- package/dist/types/src/capabilities/intent-resolver.d.ts +4 -0
- package/dist/types/src/capabilities/intent-resolver.d.ts.map +1 -0
- package/dist/types/src/capabilities/markdown.d.ts +4 -0
- package/dist/types/src/capabilities/markdown.d.ts.map +1 -0
- package/dist/types/src/capabilities/react-surface.d.ts +4 -0
- package/dist/types/src/capabilities/react-surface.d.ts.map +1 -0
- package/dist/types/src/components/FileContainer.d.ts +8 -0
- package/dist/types/src/components/FileContainer.d.ts.map +1 -0
- package/dist/types/src/components/FileInput.d.ts +8 -0
- package/dist/types/src/components/FileInput.d.ts.map +1 -0
- package/dist/types/src/components/index.d.ts +4 -11
- package/dist/types/src/components/index.d.ts.map +1 -1
- package/dist/types/src/extensions/image.d.ts +5 -0
- package/dist/types/src/extensions/image.d.ts.map +1 -1
- package/dist/types/src/extensions/image.stories.d.ts.map +1 -1
- package/dist/types/src/helpers/common.d.ts.map +1 -0
- package/dist/types/src/helpers/index.d.ts +5 -0
- package/dist/types/src/helpers/index.d.ts.map +1 -0
- package/dist/types/src/helpers/load.d.ts +13 -0
- package/dist/types/src/helpers/load.d.ts.map +1 -0
- package/dist/types/src/helpers/upload.d.ts +15 -0
- package/dist/types/src/helpers/upload.d.ts.map +1 -0
- package/dist/types/src/helpers/wnfs-url.d.ts +11 -0
- package/dist/types/src/helpers/wnfs-url.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/meta.d.ts +1 -3
- package/dist/types/src/meta.d.ts.map +1 -1
- package/dist/types/src/translations.d.ts +14 -0
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/types/file.d.ts +4 -6
- package/dist/types/src/types/file.d.ts.map +1 -1
- package/dist/types/src/types/types.d.ts +49 -6
- package/dist/types/src/types/types.d.ts.map +1 -1
- package/dist/types/tsconfig.tsbuildinfo +1 -1
- package/package.json +28 -26
- package/src/WnfsPlugin.tsx +77 -86
- package/src/blockstore.ts +151 -21
- package/src/capabilities/blockstore.ts +18 -0
- package/src/capabilities/capabilities.ts +18 -0
- package/src/capabilities/file-uploader.ts +24 -0
- package/src/capabilities/index.ts +13 -0
- package/src/capabilities/intent-resolver.ts +26 -0
- package/src/capabilities/markdown.ts +25 -0
- package/src/capabilities/react-surface.tsx +41 -0
- package/src/components/FileContainer.tsx +46 -0
- package/src/components/FileInput.tsx +46 -0
- package/src/components/index.ts +3 -4
- package/src/extensions/image.stories.tsx +26 -68
- package/src/extensions/image.tsx +203 -0
- package/src/{common.ts → helpers/common.ts} +1 -1
- package/src/helpers/index.ts +8 -0
- package/src/{load.ts → helpers/load.ts} +38 -9
- package/src/{upload.ts → helpers/upload.ts} +18 -4
- package/src/helpers/wnfs-url.ts +36 -0
- package/src/index.ts +2 -4
- package/src/meta.ts +1 -2
- package/src/translations.ts +5 -0
- package/src/types/file.ts +2 -3
- package/src/types/types.ts +30 -19
- package/dist/lib/browser/FileCard-HJIZDWJF.mjs +0 -39
- package/dist/lib/browser/FileCard-HJIZDWJF.mjs.map +0 -7
- package/dist/lib/browser/FileMain-WEWTX7QB.mjs +0 -27
- package/dist/lib/browser/FileMain-WEWTX7QB.mjs.map +0 -7
- package/dist/lib/browser/FileSection-BEUGJ7EI.mjs +0 -24
- package/dist/lib/browser/FileSection-BEUGJ7EI.mjs.map +0 -7
- package/dist/lib/browser/FileSlide-NYBGZEZS.mjs +0 -21
- package/dist/lib/browser/FileSlide-NYBGZEZS.mjs.map +0 -7
- package/dist/lib/browser/chunk-2I63TM7M.mjs +0 -27
- package/dist/lib/browser/chunk-2I63TM7M.mjs.map +0 -7
- package/dist/lib/browser/chunk-JDSKIDDR.mjs +0 -30
- package/dist/lib/browser/chunk-JDSKIDDR.mjs.map +0 -7
- package/dist/lib/browser/chunk-VL3H7P4F.mjs.map +0 -7
- package/dist/lib/browser/meta.mjs +0 -9
- package/dist/lib/browser/meta.mjs.map +0 -7
- package/dist/types/src/common.d.ts.map +0 -1
- package/dist/types/src/components/FileCard.d.ts +0 -9
- package/dist/types/src/components/FileCard.d.ts.map +0 -1
- package/dist/types/src/components/FileMain.d.ts +0 -7
- package/dist/types/src/components/FileMain.d.ts.map +0 -1
- package/dist/types/src/components/FileSection.d.ts +0 -8
- package/dist/types/src/components/FileSection.d.ts.map +0 -1
- package/dist/types/src/components/FileSlide.d.ts +0 -8
- package/dist/types/src/components/FileSlide.d.ts.map +0 -1
- package/dist/types/src/load.d.ts +0 -8
- package/dist/types/src/load.d.ts.map +0 -1
- package/dist/types/src/upload.d.ts +0 -10
- package/dist/types/src/upload.d.ts.map +0 -1
- package/dist/types/src/wnfs-url.d.ts +0 -2
- package/dist/types/src/wnfs-url.d.ts.map +0 -1
- package/src/components/FileCard.tsx +0 -45
- package/src/components/FileMain.tsx +0 -28
- package/src/components/FileSection.tsx +0 -20
- package/src/components/FileSlide.tsx +0 -20
- package/src/extensions/image.ts +0 -244
- package/src/wnfs-url.ts +0 -7
- /package/dist/types/src/{common.d.ts → helpers/common.d.ts} +0 -0
package/src/extensions/image.ts
DELETED
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
//
|
|
2
|
-
// Copyright 2024 DXOS.org
|
|
3
|
-
//
|
|
4
|
-
|
|
5
|
-
import { syntaxTree } from '@codemirror/language';
|
|
6
|
-
import { type EditorState, type Extension, StateField, type Range, StateEffect } from '@codemirror/state';
|
|
7
|
-
import { Decoration, EditorView, WidgetType, ViewPlugin, type ViewUpdate } from '@codemirror/view';
|
|
8
|
-
import type { Blockstore } from 'interface-blockstore';
|
|
9
|
-
import type { PrivateDirectory, PrivateForest } from 'wnfs';
|
|
10
|
-
|
|
11
|
-
import { log } from '@dxos/log';
|
|
12
|
-
import { type Space } from '@dxos/react-client/echo';
|
|
13
|
-
|
|
14
|
-
import { store } from '../common';
|
|
15
|
-
import { loadWnfs } from '../load';
|
|
16
|
-
|
|
17
|
-
export type ImageOptions = {
|
|
18
|
-
blockstore: Blockstore;
|
|
19
|
-
space: Space;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const image = (options: ImageOptions): Extension[] => {
|
|
23
|
-
return [stateField(options), viewPlugin(options)];
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// GLOBAL STATE
|
|
27
|
-
|
|
28
|
-
/** Indexed by space id */
|
|
29
|
-
const loadedWnfsInstances: Record<string, { directory: PrivateDirectory; forest: PrivateForest }> = {};
|
|
30
|
-
|
|
31
|
-
// EFFECT + EXTENSIONS
|
|
32
|
-
|
|
33
|
-
const stateEffect = StateEffect.define<{
|
|
34
|
-
decorations: Range<Decoration>[];
|
|
35
|
-
from: number;
|
|
36
|
-
to: number;
|
|
37
|
-
}>({});
|
|
38
|
-
|
|
39
|
-
const stateField = (options: ImageOptions) =>
|
|
40
|
-
StateField.define({
|
|
41
|
-
create: () => {
|
|
42
|
-
return Decoration.none;
|
|
43
|
-
},
|
|
44
|
-
update: (value, tr) => {
|
|
45
|
-
for (const effect of tr.effects) {
|
|
46
|
-
if (effect.is(stateEffect)) {
|
|
47
|
-
return value.map(tr.changes).update({
|
|
48
|
-
filterFrom: effect.value.from,
|
|
49
|
-
filterTo: effect.value.to,
|
|
50
|
-
filter: () => false,
|
|
51
|
-
add: effect.value.decorations,
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
return value;
|
|
57
|
-
},
|
|
58
|
-
provide: (f) => {
|
|
59
|
-
return EditorView.decorations.from(f);
|
|
60
|
-
},
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
const viewPlugin = (options: ImageOptions) =>
|
|
64
|
-
ViewPlugin.define((view: EditorView) => {
|
|
65
|
-
// Initial decoration set on document load
|
|
66
|
-
buildDecorations({
|
|
67
|
-
from: 0,
|
|
68
|
-
to: view.state.doc.length,
|
|
69
|
-
state: view.state,
|
|
70
|
-
options,
|
|
71
|
-
})
|
|
72
|
-
.then((decorations) => {
|
|
73
|
-
view.dispatch({
|
|
74
|
-
effects: stateEffect.of({
|
|
75
|
-
decorations,
|
|
76
|
-
from: 0,
|
|
77
|
-
to: view.state.doc.length,
|
|
78
|
-
}),
|
|
79
|
-
});
|
|
80
|
-
})
|
|
81
|
-
.catch((err) => {
|
|
82
|
-
throw new Error(err);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const update = (update: ViewUpdate) => {
|
|
86
|
-
if (!update.docChanged) {
|
|
87
|
-
return;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
const { from, to } = changedRange(update);
|
|
91
|
-
buildDecorations({
|
|
92
|
-
from,
|
|
93
|
-
to,
|
|
94
|
-
state: view.state,
|
|
95
|
-
options,
|
|
96
|
-
})
|
|
97
|
-
.then((decorations) => {
|
|
98
|
-
view.dispatch({
|
|
99
|
-
effects: stateEffect.of({
|
|
100
|
-
decorations,
|
|
101
|
-
from,
|
|
102
|
-
to,
|
|
103
|
-
}),
|
|
104
|
-
});
|
|
105
|
-
})
|
|
106
|
-
.catch((err) => {
|
|
107
|
-
log.catch(err);
|
|
108
|
-
});
|
|
109
|
-
};
|
|
110
|
-
|
|
111
|
-
return { update };
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
// 🛠️
|
|
115
|
-
|
|
116
|
-
const changedRange = (update: ViewUpdate) => {
|
|
117
|
-
// Find range of changes and cursor changes.
|
|
118
|
-
const cursor = update.state.selection.main.head;
|
|
119
|
-
const oldCursor = update.changes.mapPos(update.startState.selection.main.head);
|
|
120
|
-
|
|
121
|
-
let from = Math.min(cursor, oldCursor);
|
|
122
|
-
let to = Math.max(cursor, oldCursor);
|
|
123
|
-
|
|
124
|
-
update.changes.iterChangedRanges((fromA, toA, fromB, toB) => {
|
|
125
|
-
from = Math.min(from, fromB);
|
|
126
|
-
to = Math.max(to, toB);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
// Expand to cover lines.
|
|
130
|
-
from = update.state.doc.lineAt(from).from;
|
|
131
|
-
to = update.state.doc.lineAt(to).to;
|
|
132
|
-
|
|
133
|
-
// Fin
|
|
134
|
-
return { from, to };
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
const buildDecorations = async ({
|
|
138
|
-
from,
|
|
139
|
-
to,
|
|
140
|
-
state,
|
|
141
|
-
options,
|
|
142
|
-
}: {
|
|
143
|
-
from: number;
|
|
144
|
-
to: number;
|
|
145
|
-
state: EditorState;
|
|
146
|
-
options: ImageOptions;
|
|
147
|
-
}) => {
|
|
148
|
-
const cursor = state.selection.main.head;
|
|
149
|
-
let nodes: { from: number; hide: boolean; to: number; url: string }[] = [];
|
|
150
|
-
|
|
151
|
-
syntaxTree(state).iterate({
|
|
152
|
-
enter: (node) => {
|
|
153
|
-
if (node.name === 'Image') {
|
|
154
|
-
const urlNode = node.node.getChild('URL');
|
|
155
|
-
if (urlNode) {
|
|
156
|
-
const hide = state.readOnly || cursor < node.from || cursor > node.to;
|
|
157
|
-
const url = state.sliceDoc(urlNode.from, urlNode.to);
|
|
158
|
-
|
|
159
|
-
if (url.startsWith('wnfs://')) {
|
|
160
|
-
nodes.push({
|
|
161
|
-
from: node.from,
|
|
162
|
-
to: node.to,
|
|
163
|
-
hide,
|
|
164
|
-
url,
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
from,
|
|
171
|
-
to,
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
// Nodes must be sorted by `from`
|
|
175
|
-
nodes = nodes.sort((a, b) => {
|
|
176
|
-
return a.from - b.from;
|
|
177
|
-
});
|
|
178
|
-
|
|
179
|
-
const decorations = await nodes.reduce(
|
|
180
|
-
async (acc, node) => {
|
|
181
|
-
const array = await acc;
|
|
182
|
-
const path = node.url
|
|
183
|
-
.replace(/^wnfs:\/\//, '')
|
|
184
|
-
.split('/')
|
|
185
|
-
.map((p) => decodeURIComponent(p));
|
|
186
|
-
|
|
187
|
-
// Cannot load images from other spaces
|
|
188
|
-
const spaceIdFromUrl = path[1];
|
|
189
|
-
if (spaceIdFromUrl !== options.space.properties.id) {
|
|
190
|
-
return array;
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
const cacheKey = options.space.properties.wnfs_private_forest_cid;
|
|
194
|
-
|
|
195
|
-
const { directory, forest } = loadedWnfsInstances[cacheKey]
|
|
196
|
-
? loadedWnfsInstances[cacheKey]
|
|
197
|
-
: await loadWnfs(options.space, options.blockstore);
|
|
198
|
-
|
|
199
|
-
loadedWnfsInstances[cacheKey] = {
|
|
200
|
-
directory,
|
|
201
|
-
forest,
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const wnfsStore = store(options.blockstore);
|
|
205
|
-
|
|
206
|
-
// Load image contents into memory blob
|
|
207
|
-
const { result } = await directory.read(path, true, forest, wnfsStore);
|
|
208
|
-
const blob = new Blob([result]);
|
|
209
|
-
const blobUrl = URL.createObjectURL(blob);
|
|
210
|
-
|
|
211
|
-
// Create decoration
|
|
212
|
-
return [
|
|
213
|
-
...array,
|
|
214
|
-
Decoration.replace({
|
|
215
|
-
block: true, // Prevent cursor from entering.
|
|
216
|
-
widget: new WnfsImageWidget(blobUrl),
|
|
217
|
-
}).range(node.hide ? node.from : node.to, node.to),
|
|
218
|
-
];
|
|
219
|
-
},
|
|
220
|
-
Promise.resolve([] as Range<Decoration>[]),
|
|
221
|
-
);
|
|
222
|
-
|
|
223
|
-
return decorations;
|
|
224
|
-
};
|
|
225
|
-
|
|
226
|
-
class WnfsImageWidget extends WidgetType {
|
|
227
|
-
constructor(readonly _url: string) {
|
|
228
|
-
super();
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
override eq(other: this) {
|
|
232
|
-
return this._url === (other as any as WnfsImageWidget)._url;
|
|
233
|
-
}
|
|
234
|
-
|
|
235
|
-
override toDOM(view: EditorView) {
|
|
236
|
-
const img = document.createElement('img');
|
|
237
|
-
img.setAttribute('loading', 'lazy');
|
|
238
|
-
img.setAttribute('src', this._url);
|
|
239
|
-
img.setAttribute('class', 'cm-image');
|
|
240
|
-
// Images are hidden until successfully loaded to avoid flickering effects.
|
|
241
|
-
img.onload = () => img.classList.add('cm-loaded-image');
|
|
242
|
-
return img;
|
|
243
|
-
}
|
|
244
|
-
}
|
package/src/wnfs-url.ts
DELETED
|
File without changes
|