-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy patheditor.ts
More file actions
169 lines (141 loc) · 4.48 KB
/
editor.ts
File metadata and controls
169 lines (141 loc) · 4.48 KB
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
163
164
165
166
167
168
169
import type { FilesRefList, Files, EditorSchema, FileDescriptor } from '@tutorialkit/types';
import { atom, map, computed } from 'nanostores';
import { EditorConfig } from '../webcontainer/editor-config.js';
export interface EditorDocument {
value: string | Uint8Array;
loading: boolean;
filePath: string;
type: FileDescriptor['type'];
scroll?: ScrollPosition;
}
export interface ScrollPosition {
top: number;
left: number;
}
export type EditorDocuments = Record<string, EditorDocument | undefined>;
export class EditorStore {
editorConfig = atom<EditorConfig>(new EditorConfig());
selectedFile = atom<string | undefined>();
documents = map<EditorDocuments>({});
files = computed(this.documents, (documents) =>
Object.entries(documents).map<FileDescriptor>(([path, doc]) => ({ path, type: doc?.type || 'file' })),
);
currentDocument = computed([this.documents, this.selectedFile], (documents, selectedFile) => {
if (!selectedFile) {
return undefined;
}
return documents[selectedFile];
});
setEditorConfig(config?: EditorSchema) {
this.editorConfig.set(new EditorConfig(config));
}
setSelectedFile(filePath: string | undefined) {
this.selectedFile.set(filePath);
}
setDocuments(files: FilesRefList | Files) {
// lesson, solution and template file entries are always files - empty folders are not supported
const type = 'file';
// check if it is a FilesRef
if (Array.isArray(files)) {
this.documents.set(
Object.fromEntries(
files[1].map((filePath) => {
return [
filePath,
{
value: '',
type,
loading: true,
filePath,
},
];
}),
),
);
} else {
const previousDocuments = this.documents.value;
this.documents.set(
Object.fromEntries(
Object.entries(files).map(([filePath, value]) => {
return [
filePath,
{
value,
type,
loading: false,
filePath,
scroll: previousDocuments?.[filePath]?.scroll,
},
];
}),
) satisfies EditorDocuments,
);
}
}
updateScrollPosition(filePath: string, position: ScrollPosition) {
const documentState = this.documents.get()[filePath];
if (!documentState) {
return;
}
this.documents.setKey(filePath, {
...documentState,
scroll: position,
});
}
addFileOrFolder(file: FileDescriptor) {
// when adding file or folder to empty folder, remove the empty folder from documents
const emptyFolder = this.files.get().find((f) => f.type === 'folder' && file.path.startsWith(f.path));
if (emptyFolder && emptyFolder.type === 'folder') {
this.documents.setKey(emptyFolder.path, undefined);
}
this.documents.setKey(file.path, {
filePath: file.path,
type: file.type,
value: '',
loading: false,
});
}
updateFile(filePath: string, content: string | Uint8Array): boolean {
const documentState = this.documents.get()[filePath];
if (!documentState) {
return false;
}
const currentContent = documentState.value;
const contentChanged = currentContent !== content;
if (contentChanged) {
this.documents.setKey(filePath, {
...documentState,
value: content,
});
}
return contentChanged;
}
onDocumentChanged(filePath: string, callback: (document: Readonly<EditorDocument>) => void) {
const unsubscribeFromCurrentDocument = this.currentDocument.subscribe((document) => {
if (document?.filePath === filePath) {
callback(document);
}
});
const unsubscribeFromDocuments = this.documents.subscribe((documents) => {
const document = documents[filePath];
/**
* We grab the document from the store, but only call the callback if it is not loading anymore which means
* the content is loaded.
*/
if (document && !document.loading) {
/**
* Call this in a `queueMicrotask` because the subscribe callback is called synchronoulsy,
* which causes the `unsubscribeFromDocuments` to not exist yet.
*/
queueMicrotask(() => {
callback(document);
unsubscribeFromDocuments();
});
}
});
return () => {
unsubscribeFromDocuments();
unsubscribeFromCurrentDocument();
};
}
}