-
Notifications
You must be signed in to change notification settings - Fork 11
/
AmpleforthViewer.ns
71 lines (68 loc) · 3.18 KB
/
AmpleforthViewer.ns
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
Newspeak3
'Root'
class AmpleforthViewer packageUsing: manifest = (
(*
An app that opens a pre-determined Ampleforth document.
Given a URI of the form primordialsoup.html/?snapshot=AmpleforthEditorApp&docName=myDoc, it can open to the desired document.
If the document is not avaliable on the server, an error document is opened with an explanation of the problem.
Ideally, we would isolate the web dependencies we have here so that the code would work on alternate platforms.
Among other things, we would need the web implementation to pass in the URI arguments to #main:args: so we would not be concerned with that here. We'd want a mature, portable Newspeak file API as well. For the foreseeable future, this will do.
*)
|
private HopscotchIDE = manifest HopscotchWebIDE packageUsing: manifest.
|
) (
missingDocument: docName <String> in: ide <HopscotchWebIDE> ^ <Document> = (
|
errorMessage = docName isEmpty
ifFalse: [ '<h1>No document named ', docName, ' found on server</h1>']
ifTrue: [ '<h1>No document specified. You must specify a document name in the URI using the docName parameter. Use the format
<br><br>
<i>primordialsoup.html/?snapshot=AmpleforthEditorApp&docName=myDoc<i>
<br><br>
where myDoc is the name of the desired document.
<br>
Make sure you didn''t write ?docName=myDoc, or mispell or omit docName.
'].
|
^ide documents freshDocumentNamed: #MissingDocumentNotice initialText: errorMessage
)
public main: platform args: args = (
|
ide = HopscotchIDE ideUsingPlatform: platform.
documents = ide documents.
(* Get the current URI and return the value of parameter 'docName', this being the name of the document we should open *)
rawName = (((platform js global at: #URL) new: ((platform js global at: #window) at: #location)) at: #searchParams) get: #docName.
(* If docName argument is missing, rawName will be nil, which would cause difficulties. Guard against this. *)
docName = rawName isNil ifFalse: [rawName] ifTrue: [''].
request = (platform js global at: #XMLHttpRequest) new.
JSZip = platform js global at: 'JSZip'.
|
ide setupNames.
ide launch: (ide browsing HomeSubject new).
request
open: 'GET' filenamed: docName, '.zip' flag: true;
at: #responseType put: 'blob';
at: #onload put: [:event |
|
jszp <JSPromise[JSZip]> = JSZip loadAsync: (request at: #response).
|
jszp then: [:jsz <JSZip> |
| namespace = ide browsing NamespaceSubject onModel: ide namespacing Root key: #Root. |
documents loadDocument: jsz named: docName into: namespace
ifSuccess: [ide browsing Utilities enterSubject: (documents DocumentSubject onModel: (namespace model at: docName))]
ifFailure: [
| failureDoc = missingDocument: docName in: ide. |
ide browsing Utilities enterSubject: (documents DocumentSubject onModel: failureDoc).
].
nil
] onError: [:ex |
| failureDoc = missingDocument: docName in: ide. |
ide browsing Utilities enterSubject: (documents DocumentSubject onModel: failureDoc).
nil
]
];
send.
)
) : (
)