-
Notifications
You must be signed in to change notification settings - Fork 1
/
folder-editor-actions.js
170 lines (154 loc) · 4.03 KB
/
folder-editor-actions.js
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
170
// @flow
import * as t from "./folder-editor-actiontypes";
import * as selectors from "./folder-editor-selectors";
import * as c from "./folder-editor-constants";
import SlateFile from "./slate-extensions/slate-file/slate-file-index";
import * as Utils from "../../utils/utils-index";
import * as FsMergedSelector from "../../filesystem/fs-merged-selectors";
import fsWatch from "../../filesystem/watch/fs-watch-index";
import nodePath from "path";
import _ from "lodash";
import { Raw, State } from "slate";
import { DEFAULT_NODE } from "./slate-extensions/rich-text-types";
import type { ThunkArgs, Action } from "../../types";
export function folderEditorInit(path: string) {
return (dispatch: Function, getState: Function) => {
const fileList = FsMergedSelector.getFiltedBaseArrayOfFolder_Factory()(
getState(),
path
);
if (
// SAVED Editor State exists?
fsWatch.selectors.getFile(
getState(),
nodePath.join(path, c.INDEX_BASE_NAME)
)
) {
const fileContent = Utils.fs
.loadFile(nodePath.join(path, c.INDEX_BASE_NAME))
.then(fileContent => {
let editorState = Raw.deserialize(JSON.parse(fileContent), {
terse: true
});
editorState = mapFilesToEditorState(fileList, editorState);
dispatch({
type: t.FOLDER_EDITOR_INIT,
payload: {
path: path,
editorState: editorState
}
});
});
} else {
dispatch({
type: t.FOLDER_EDITOR_INIT,
payload: {
path: path,
editorState: newStateWithFileNodes(fileList)
}
});
}
};
}
export function folderEditorClose(path: string): Action {
return {
type: t.FOLDER_EDITOR_CLOSE,
payload: {
path: path
}
};
}
export function folderEditorChange(
path: string,
editorState: Class<State>
): Action {
return {
type: t.FOLDER_EDITOR_CHANGE,
payload: {
path: path,
editorState: editorState,
selectedFiles: SlateFile.utils.buildSelectedFiles(editorState)
}
};
}
function mapFilesToEditorState(
fileList: Array<string>,
editorState: Class<State>
): Class<State> {
const filesOnDisk = fileList;
const filesInEditor = SlateFile.utils.getFilesInState(editorState);
const filesNotInEditor = _.difference(filesOnDisk, filesInEditor);
const fileNotOnDisk = _.difference(filesInEditor, filesOnDisk);
let transforming = editorState.transform();
fileNotOnDisk.forEach((base, index) => {
transforming = SlateFile.stateTransforms.removeExisting(transforming, base);
});
filesNotInEditor.forEach((base, index) => {
if (base != c.INDEX_BASE_NAME) {
transforming = SlateFile.stateTransforms.insertFileAtEnd(
transforming,
editorState.get("document"),
base
);
}
});
editorState = transforming.apply({ save: false });
return editorState;
}
// Faster than block transforms, but still slow
function newStateWithFileNodes(filesNotInEditor): Class<State> {
let state = {
document: {
data: {},
kind: "document",
nodes: []
},
kind: "state"
};
filesNotInEditor.forEach((base, index) => {
if (base != c.INDEX_BASE_NAME) {
state.document.nodes.push({
kind: "block",
type: c.BLOCK_TYPE_FILE,
isVoid: true,
nodes: [
{
kind: "text",
ranges: [
{
kind: "range",
text: " ",
marks: []
}
]
}
],
data: {
base: base
}
});
}
});
//Add empty line at the end
state.document.nodes.push({
kind: "block",
type: DEFAULT_NODE.type,
nodes: [
{
kind: "text",
ranges: [
{
kind: "range",
text: "",
marks: []
}
]
}
]
});
const editorState: Class<State> = Raw.deserialize(state, {
terse: false,
normalize: false
});
return editorState;
}