Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagecell
Path: blob/master/js/main.js
447 views
1
import sagecell from "./sagecell";
2
import cell from "./cell";
3
import "./jquery-global";
4
5
import { console } from "./console";
6
7
(function () {
8
var ga = document.createElement("script");
9
ga.type = "text/javascript";
10
ga.async = true;
11
ga.src =
12
("https:" == document.location.protocol
13
? "https://ssl"
14
: "http://www") + ".google-analytics.com/ga.js";
15
var s = document.getElementsByTagName("script")[0];
16
s.parentNode.insertBefore(ga, s);
17
})();
18
19
/**
20
* Creates a promise and hoists its `resolve` method so that
21
* it can be called externally.
22
*/
23
function makeResolvablePromise() {
24
const ret = { promise: null, resolve: null, state: "pending" };
25
ret.promise = new Promise((resolve) => {
26
ret.resolve = (...args) => {
27
ret.state = "fulfilled";
28
return resolve(...args);
29
};
30
});
31
return ret;
32
}
33
34
// Set up the global sagecell variable. This needs to be done right away because other
35
// scripts want to access window.sagecell.
36
Object.assign(sagecell, {
37
templates: {
38
minimal: {
39
// for an evaluate button and nothing else.
40
editor: "textarea-readonly",
41
hide: ["editor", "files", "permalink"],
42
},
43
restricted: {
44
// to display/evaluate code that can't be edited.
45
editor: "codemirror-readonly",
46
hide: ["files", "permalink"],
47
},
48
},
49
allLanguages: [
50
"sage",
51
"gap",
52
"gp",
53
"html",
54
"macaulay2",
55
"maxima",
56
"octave",
57
"python",
58
"r",
59
"singular",
60
],
61
// makeSagecell must be available as soon as the script loads,
62
// but we may not be ready to process data right away, so we
63
// provide a wrapper that will poll until sagecell is ready.
64
makeSagecell: function (args) {
65
// Clients expect to receive a `cellInfo` object right away.
66
// However, this object cannot be made available until the page loads.
67
// If we're not ready, we return a stub object that gets updated with
68
// the proper data when it becomes available.
69
if (sagecell._initPromise.state === "pending") {
70
const ret = {};
71
sagecell._initPromise.promise
72
.then(() => {
73
const cellInfo = window.sagecell._makeSagecell(args);
74
Object.assign(ret, cellInfo);
75
})
76
.catch((e) => {
77
console.warn("Encountered error in makeSagecell", e);
78
});
79
return ret;
80
} else {
81
return window.sagecell._makeSagecell(args);
82
}
83
},
84
_initPromise: makeResolvablePromise(),
85
quietMode: false,
86
});
87
88
// Purely for backwards compatibility
89
window.singlecell = sagecell;
90
window.singlecell.makeSinglecell = window.singlecell.makeSagecell;
91
92
/**
93
* Retrieve the kernel index associated with `key`. If
94
* needed, this function will push `null` onto the kernel
95
* stack, providing a space for the kernel to be initialized.
96
*/
97
function linkKeyToIndex(key) {
98
sagecell.linkKeys = sagecell.linkKeys || {};
99
if (key in sagecell.linkKeys) {
100
return sagecell.linkKeys[key];
101
}
102
103
sagecell.kernels = sagecell.kernels || [];
104
// Make sure we have a kernel to share for our new key.
105
const index = sagecell.kernels.push(null) - 1;
106
sagecell.linkKeys[key] = index;
107
return index;
108
}
109
110
sagecell._makeSagecell = function (args) {
111
console.info("sagecell.makeSagecell called");
112
// If `args.linkKey` is set, we force the `linked` option to be true.
113
if (args.linkKey) {
114
args = Object.assign({}, args, { linked: true });
115
}
116
117
var cellInfo = {};
118
if (args.linked && args.linkKey) {
119
cell.make(args, cellInfo, linkKeyToIndex(args.linkKey));
120
} else {
121
cell.make(args, cellInfo);
122
}
123
console.info("sagecell.makeSagecell finished");
124
return cellInfo;
125
};
126
sagecell.deleteSagecell = function (cellInfo) {
127
cell.delete(cellInfo);
128
};
129
sagecell.moveInputForm = function (cellInfo) {
130
cell.moveInputForm(cellInfo);
131
};
132
sagecell.restoreInputForm = function (cellInfo) {
133
cell.restoreInputForm(cellInfo);
134
};
135
136
sagecell._initPromise.resolve();
137
138
export default sagecell;
139
export { sagecell };
140
141