-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrunestone-slice.ts
178 lines (165 loc) · 5.83 KB
/
runestone-slice.ts
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
171
172
173
174
175
176
177
178
import {
createDraftSafeSelector,
createSlice,
PayloadAction,
} from "@reduxjs/toolkit";
import { createLoggingAsyncThunk } from "../../hooks";
import { RootState } from "../../store";
import { ACFactory as ACFactoryType, EBookConfig } from "./types";
// Some globals may exist from Runestone
declare const eBookConfig: EBookConfig | undefined;
declare const ACFactory: ACFactoryType | undefined;
declare const rsMathReady: Function | undefined;
export interface RunestoneState {
runestoneEnabled: boolean;
activeEbookConfig: EBookConfig;
scratchActiveCodeVisible: boolean;
}
const initialState: RunestoneState = {
runestoneEnabled: true,
scratchActiveCodeVisible: false,
activeEbookConfig: {
useRunestoneServices: false,
host: "",
app: "",
course: "",
basecourse: "",
isLoggedIn: false,
email: "",
isInstructor: false,
logLevel: 0,
ajaxURL: "",
username: "",
readings: [],
activities: {},
downloadsEnabled: false,
allow_pairs: false,
enableScratchAC: false,
acDefaultLanguage: "",
new_server_prefix: "",
runestone_version: "",
// This is normally dynamically created by Runestone.
scratchDiv: "",
},
};
export interface PermalinkDetails {
title: string | undefined;
url: string;
}
const runestoneThunks = {
runestoneInit: createLoggingAsyncThunk(
"runestone/runestoneInit",
async (_: void, { dispatch, getState }) => {
// Test whether or not Runestone is present on the page by looking for
// the presence rsMathReady
// XXX: this should be done in a more reliable way.
if (typeof rsMathReady !== "undefined") {
const eBookConfig: EBookConfig =
(window as any).eBookConfig || {};
dispatch(
runestoneActions.setRunestoneEnabled(
Boolean(eBookConfig.useRunestoneServices)
)
);
dispatch(runestoneActions.setActiveEbookConfig(eBookConfig));
}
}
),
runestonePopupScratchActiveCode: createLoggingAsyncThunk(
"runestone/popupScratchActiveCode",
async (_: void, { dispatch, getState }) => {
if (
typeof ACFactory === "undefined" ||
typeof eBookConfig === "undefined"
) {
console.warn(
"Asked to start scratch active code window, but ACAFactor is undefined"
);
return;
}
dispatch(runestoneActions.setScratchActiveCodeVisible(true));
const localEbookConfig = runestoneEbookConfigSelector(
getState() as any
);
// If this function is called, we force the scratch active code window to be enabled.
// Since we shouldn't be able to call it if active code was disabled in the first place.
eBookConfig.enableScratchAC = true;
// Follow the code from Runestone's popupScratchAC
if (!localEbookConfig.scratchDiv) {
ACFactory.createScratchActivecode();
const newId = eBookConfig.scratchDiv;
if (newId) {
dispatch(
runestoneActions.setActiveEbookConfig({
scratchDiv: newId,
})
);
}
const container = document.getElementById(
"runestone-scratch-active-code-container-id"
);
const activeCodeDiv = document.getElementById(newId);
if (!container || !activeCodeDiv) {
console.warn(
"Cannot find created ActiveCode Container or ActiveCode div with id:",
newId
);
return;
}
container.appendChild(activeCodeDiv);
const acInstance = ACFactory.createActiveCode(
activeCodeDiv,
localEbookConfig.acDefaultLanguage
);
if (eBookConfig.isLoggedIn) {
acInstance.enableSaveLoad();
}
}
}
),
};
export const runestoneSlice = createSlice({
name: "global",
initialState,
// The `reducers` field lets us define reducers and generate associated actions
reducers: {
setRunestoneEnabled(state, action: PayloadAction<boolean>) {
state.runestoneEnabled = action.payload;
},
setActiveEbookConfig(
state,
action: PayloadAction<Partial<EBookConfig>>
) {
Object.assign(state.activeEbookConfig, action.payload);
},
setScratchActiveCodeVisible(state, action: PayloadAction<boolean>) {
state.scratchActiveCodeVisible = action.payload;
},
},
});
export const runestoneActions = {
...runestoneSlice.actions,
...runestoneThunks,
};
const selfSelector = (state: RootState) => state.runestone;
export const runestoneEnabledSelector = createDraftSafeSelector(
selfSelector,
(state) => state.runestoneEnabled
);
export const runestoneIsStudentSelector = createDraftSafeSelector(
selfSelector,
(state) => !state.activeEbookConfig.isInstructor
);
export const runestoneIsInstructorSelector = createDraftSafeSelector(
selfSelector,
(state) => state.activeEbookConfig.isInstructor
);
export const runestoneScratchActiveCodeVisibleSelector =
createDraftSafeSelector(
selfSelector,
(state) => state.scratchActiveCodeVisible
);
export const runestoneEbookConfigSelector = createDraftSafeSelector(
selfSelector,
(state) => state.activeEbookConfig
);