@dxos/plugin-wnfs 0.7.5-main.9cb18ac → 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.
Files changed (135) hide show
  1. package/dist/lib/browser/FileContainer-I2P6I7CX.mjs +94 -0
  2. package/dist/lib/browser/FileContainer-I2P6I7CX.mjs.map +7 -0
  3. package/dist/lib/browser/blockstore-6U5XTSIH.mjs +192 -0
  4. package/dist/lib/browser/blockstore-6U5XTSIH.mjs.map +7 -0
  5. package/dist/lib/browser/chunk-357BWHML.mjs +16 -0
  6. package/dist/lib/browser/chunk-357BWHML.mjs.map +7 -0
  7. package/dist/lib/browser/{chunk-VL3H7P4F.mjs → chunk-6YFT42F7.mjs} +5 -8
  8. package/dist/lib/browser/chunk-6YFT42F7.mjs.map +7 -0
  9. package/dist/lib/browser/chunk-EMRQJXBW.mjs +60 -0
  10. package/dist/lib/browser/chunk-EMRQJXBW.mjs.map +7 -0
  11. package/dist/lib/browser/chunk-SCQWJNHD.mjs +157 -0
  12. package/dist/lib/browser/chunk-SCQWJNHD.mjs.map +7 -0
  13. package/dist/lib/browser/chunk-YLGCDZV3.mjs +16 -0
  14. package/dist/lib/browser/chunk-YLGCDZV3.mjs.map +7 -0
  15. package/dist/lib/browser/file-uploader-EDHMSKYS.mjs +29 -0
  16. package/dist/lib/browser/file-uploader-EDHMSKYS.mjs.map +7 -0
  17. package/dist/lib/browser/index.mjs +96 -486
  18. package/dist/lib/browser/index.mjs.map +4 -4
  19. package/dist/lib/browser/intent-resolver-WDO4EIG3.mjs +43 -0
  20. package/dist/lib/browser/intent-resolver-WDO4EIG3.mjs.map +7 -0
  21. package/dist/lib/browser/markdown-MGSH5CVQ.mjs +198 -0
  22. package/dist/lib/browser/markdown-MGSH5CVQ.mjs.map +7 -0
  23. package/dist/lib/browser/meta.json +1 -1
  24. package/dist/lib/browser/react-surface-EBQICOUA.mjs +81 -0
  25. package/dist/lib/browser/react-surface-EBQICOUA.mjs.map +7 -0
  26. package/dist/lib/browser/types/index.mjs +2 -2
  27. package/dist/types/src/WnfsPlugin.d.ts +1 -3
  28. package/dist/types/src/WnfsPlugin.d.ts.map +1 -1
  29. package/dist/types/src/blockstore.d.ts +4 -1
  30. package/dist/types/src/blockstore.d.ts.map +1 -1
  31. package/dist/types/src/capabilities/blockstore.d.ts +4 -0
  32. package/dist/types/src/capabilities/blockstore.d.ts.map +1 -0
  33. package/dist/types/src/capabilities/capabilities.d.ts +12 -0
  34. package/dist/types/src/capabilities/capabilities.d.ts.map +1 -0
  35. package/dist/types/src/capabilities/file-uploader.d.ts +4 -0
  36. package/dist/types/src/capabilities/file-uploader.d.ts.map +1 -0
  37. package/dist/types/src/capabilities/index.d.ts +7 -0
  38. package/dist/types/src/capabilities/index.d.ts.map +1 -0
  39. package/dist/types/src/capabilities/intent-resolver.d.ts +4 -0
  40. package/dist/types/src/capabilities/intent-resolver.d.ts.map +1 -0
  41. package/dist/types/src/capabilities/markdown.d.ts +4 -0
  42. package/dist/types/src/capabilities/markdown.d.ts.map +1 -0
  43. package/dist/types/src/capabilities/react-surface.d.ts +4 -0
  44. package/dist/types/src/capabilities/react-surface.d.ts.map +1 -0
  45. package/dist/types/src/components/FileContainer.d.ts +8 -0
  46. package/dist/types/src/components/FileContainer.d.ts.map +1 -0
  47. package/dist/types/src/components/FileInput.d.ts +8 -0
  48. package/dist/types/src/components/FileInput.d.ts.map +1 -0
  49. package/dist/types/src/components/index.d.ts +4 -11
  50. package/dist/types/src/components/index.d.ts.map +1 -1
  51. package/dist/types/src/extensions/image.d.ts +5 -0
  52. package/dist/types/src/extensions/image.d.ts.map +1 -1
  53. package/dist/types/src/extensions/image.stories.d.ts.map +1 -1
  54. package/dist/types/src/helpers/common.d.ts.map +1 -0
  55. package/dist/types/src/helpers/index.d.ts +5 -0
  56. package/dist/types/src/helpers/index.d.ts.map +1 -0
  57. package/dist/types/src/helpers/load.d.ts +13 -0
  58. package/dist/types/src/helpers/load.d.ts.map +1 -0
  59. package/dist/types/src/helpers/upload.d.ts +15 -0
  60. package/dist/types/src/helpers/upload.d.ts.map +1 -0
  61. package/dist/types/src/helpers/wnfs-url.d.ts +11 -0
  62. package/dist/types/src/helpers/wnfs-url.d.ts.map +1 -0
  63. package/dist/types/src/index.d.ts +2 -2
  64. package/dist/types/src/index.d.ts.map +1 -1
  65. package/dist/types/src/meta.d.ts +1 -3
  66. package/dist/types/src/meta.d.ts.map +1 -1
  67. package/dist/types/src/translations.d.ts +14 -0
  68. package/dist/types/src/translations.d.ts.map +1 -1
  69. package/dist/types/src/types/file.d.ts +4 -6
  70. package/dist/types/src/types/file.d.ts.map +1 -1
  71. package/dist/types/src/types/types.d.ts +49 -6
  72. package/dist/types/src/types/types.d.ts.map +1 -1
  73. package/dist/types/tsconfig.tsbuildinfo +1 -1
  74. package/package.json +28 -26
  75. package/src/WnfsPlugin.tsx +77 -86
  76. package/src/blockstore.ts +151 -21
  77. package/src/capabilities/blockstore.ts +18 -0
  78. package/src/capabilities/capabilities.ts +18 -0
  79. package/src/capabilities/file-uploader.ts +24 -0
  80. package/src/capabilities/index.ts +13 -0
  81. package/src/capabilities/intent-resolver.ts +26 -0
  82. package/src/capabilities/markdown.ts +25 -0
  83. package/src/capabilities/react-surface.tsx +41 -0
  84. package/src/components/FileContainer.tsx +46 -0
  85. package/src/components/FileInput.tsx +46 -0
  86. package/src/components/index.ts +3 -4
  87. package/src/extensions/image.stories.tsx +26 -68
  88. package/src/extensions/image.tsx +203 -0
  89. package/src/{common.ts → helpers/common.ts} +1 -1
  90. package/src/helpers/index.ts +8 -0
  91. package/src/{load.ts → helpers/load.ts} +38 -9
  92. package/src/{upload.ts → helpers/upload.ts} +18 -4
  93. package/src/helpers/wnfs-url.ts +36 -0
  94. package/src/index.ts +2 -4
  95. package/src/meta.ts +1 -2
  96. package/src/translations.ts +5 -0
  97. package/src/types/file.ts +2 -3
  98. package/src/types/types.ts +30 -19
  99. package/dist/lib/browser/FileCard-HJIZDWJF.mjs +0 -39
  100. package/dist/lib/browser/FileCard-HJIZDWJF.mjs.map +0 -7
  101. package/dist/lib/browser/FileMain-WEWTX7QB.mjs +0 -27
  102. package/dist/lib/browser/FileMain-WEWTX7QB.mjs.map +0 -7
  103. package/dist/lib/browser/FileSection-BEUGJ7EI.mjs +0 -24
  104. package/dist/lib/browser/FileSection-BEUGJ7EI.mjs.map +0 -7
  105. package/dist/lib/browser/FileSlide-NYBGZEZS.mjs +0 -21
  106. package/dist/lib/browser/FileSlide-NYBGZEZS.mjs.map +0 -7
  107. package/dist/lib/browser/chunk-2I63TM7M.mjs +0 -27
  108. package/dist/lib/browser/chunk-2I63TM7M.mjs.map +0 -7
  109. package/dist/lib/browser/chunk-JDSKIDDR.mjs +0 -30
  110. package/dist/lib/browser/chunk-JDSKIDDR.mjs.map +0 -7
  111. package/dist/lib/browser/chunk-VL3H7P4F.mjs.map +0 -7
  112. package/dist/lib/browser/meta.mjs +0 -9
  113. package/dist/lib/browser/meta.mjs.map +0 -7
  114. package/dist/types/src/common.d.ts.map +0 -1
  115. package/dist/types/src/components/FileCard.d.ts +0 -9
  116. package/dist/types/src/components/FileCard.d.ts.map +0 -1
  117. package/dist/types/src/components/FileMain.d.ts +0 -7
  118. package/dist/types/src/components/FileMain.d.ts.map +0 -1
  119. package/dist/types/src/components/FileSection.d.ts +0 -8
  120. package/dist/types/src/components/FileSection.d.ts.map +0 -1
  121. package/dist/types/src/components/FileSlide.d.ts +0 -8
  122. package/dist/types/src/components/FileSlide.d.ts.map +0 -1
  123. package/dist/types/src/load.d.ts +0 -8
  124. package/dist/types/src/load.d.ts.map +0 -1
  125. package/dist/types/src/upload.d.ts +0 -10
  126. package/dist/types/src/upload.d.ts.map +0 -1
  127. package/dist/types/src/wnfs-url.d.ts +0 -2
  128. package/dist/types/src/wnfs-url.d.ts.map +0 -1
  129. package/src/components/FileCard.tsx +0 -45
  130. package/src/components/FileMain.tsx +0 -28
  131. package/src/components/FileSection.tsx +0 -20
  132. package/src/components/FileSlide.tsx +0 -20
  133. package/src/extensions/image.ts +0 -244
  134. package/src/wnfs-url.ts +0 -7
  135. /package/dist/types/src/{common.d.ts → helpers/common.d.ts} +0 -0
@@ -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
@@ -1,7 +0,0 @@
1
- //
2
- // Copyright 2024 DXOS.org
3
- //
4
-
5
- export const wnfsUrl = (filePath: string[]) => {
6
- return 'wnfs://' + filePath.map((f) => encodeURIComponent(f)).join('/');
7
- };