-
-
Notifications
You must be signed in to change notification settings - Fork 553
/
notebook.js
126 lines (104 loc) · 3.48 KB
/
notebook.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
// @flow
import * as Immutable from "immutable";
import * as commutable from "@nteract/commutable";
import type { CellID } from "@nteract/commutable";
import { createSelector } from "reselect";
// All these selectors expect a NotebookModel as the top level state
import type { NotebookModel } from "../state/entities/contents/notebook";
export const cellMap = (model: NotebookModel) =>
model.notebook.get("cellMap", Immutable.Map());
export const cellById = (model: NotebookModel, { id }: { id: CellID }) =>
cellMap(model).get(id);
export const cellOrder = (model: NotebookModel): Immutable.List<CellID> =>
model.notebook.get("cellOrder", Immutable.List());
export const cellFocused = (model: NotebookModel): ?CellID => model.cellFocused;
export const editorFocusedId = (model: NotebookModel): ?CellID =>
model.editorFocused;
export const codeCellIdsBelow = (model: NotebookModel) => {
const cellFocused = model.cellFocused;
if (!cellFocused) {
// NOTE: if there is no focused cell, this runs none of the cells
return Immutable.List();
}
const cellOrder = model.notebook.get("cellOrder", Immutable.List());
const index = cellOrder.indexOf(cellFocused);
return cellOrder
.skip(index)
.filter(
id => model.notebook.getIn(["cellMap", id, "cell_type"]) === "code"
);
};
export const hiddenCellIds = createSelector(
[cellMap, cellOrder],
(cellMap, cellOrder) => {
return cellOrder.filter(id =>
cellMap.getIn([id, "metadata", "inputHidden"])
);
}
);
export const idsOfHiddenOutputs = createSelector(
[cellMap, cellOrder],
(cellMap, cellOrder): Immutable.List<any> => {
if (!cellOrder || !cellMap) {
return Immutable.List();
}
return cellOrder.filter(cellId =>
cellMap.getIn([cellId, "metadata", "outputHidden"])
);
}
);
export const transientCellMap = (model: NotebookModel) =>
model.transient.get("cellMap", Immutable.Map());
export const codeCellIds = createSelector(
[cellMap, cellOrder],
(cellMap, cellOrder) => {
return cellOrder.filter(id => cellMap.getIn([id, "cell_type"]) === "code");
}
);
export const metadata = (model: NotebookModel) =>
model.notebook.get("metadata", Immutable.Map());
export const githubUsername = createSelector(
[metadata],
metadata => metadata.get("github_username", null)
);
export const gistId = createSelector(
[metadata],
metadata => metadata.get("gist_id", null)
);
export const notebook = (model: NotebookModel) => model.notebook;
export const savedNotebook = (model: NotebookModel) => model.savedNotebook;
export const isDirty = createSelector(
notebook,
savedNotebook,
(original, disk) => !Immutable.is(original, disk)
);
export const asJSON = createSelector(
[notebook],
notebook => {
return commutable.toJS(notebook);
}
);
// NOTE: This is called asString instead of toString so that REPLs
// don't think of this as the representation of this module
export const asString = createSelector(
[asJSON],
notebookJS => {
if (notebookJS) {
return commutable.stringifyNotebook(notebookJS);
}
return "";
}
);
const CODE_MIRROR_MODE_DEFAULT = "text";
export const codeMirrorMode = createSelector(
metadata,
metadata =>
metadata.getIn(["language_info", "codemirror_mode"]) ||
metadata.getIn(["kernel_info", "language"]) ||
metadata.getIn(["kernelspec", "language"]) ||
CODE_MIRROR_MODE_DEFAULT
);
export const displayName = createSelector(
[metadata],
metadata => metadata.getIn(["kernelspec", "display_name"], "")
);