Skip to content

Commit

Permalink
Merge pull request #10 from JerryI/dev2
Browse files Browse the repository at this point in the history
Dev2
  • Loading branch information
JerryI authored Oct 18, 2024
2 parents 5dd0150 + c40f75f commit 51e880e
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 4 deletions.
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wljs-editor",
"version": "1.1.8",
"version": "1.1.9",
"description": "WLJS Code editor",
"scripts": {
"build": "node --max-old-space-size=8192 ./node_modules/.bin/rollup --config rollup.config.mjs",
Expand Down Expand Up @@ -38,7 +38,8 @@
"src/Notifications.wl",
"src/Autocomplete.wl",
"src/ContextMenu.wl",
"src/FileUpload.wl"
"src/FileUpload.wl",
"src/FrontendRuntime.wl"
],
"kernel": [
"src/FrontendObject.wl",
Expand All @@ -56,7 +57,8 @@
"src/MetaMarkersKernel.wl",
"src/AutocompleteKernel.wl",
"src/RasterizeKernel.wl",
"src/System.wl"
"src/System.wl",
"src/FrontendRuntimeKernel.wl"
],
"styles": "src/styles.css",
"priority": -10,
Expand Down
1 change: 0 additions & 1 deletion src/EditorKernel.wl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ CellView::usage = "A view component for an input or output cell CellView[_String

InputEditor::usage = "InputEditor[string_] _EventObject"


Begin["`Private`"]


Expand Down
65 changes: 65 additions & 0 deletions src/FrontendRuntime.wl
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

BeginPackage["Notebook`Editor`FrontEndRuntime`", {
"JerryI`Misc`Events`",
"JerryI`Misc`Events`Promise`",
"JerryI`Misc`WLJS`Transport`",
"JerryI`Notebook`Kernel`",
"JerryI`Notebook`",
"JerryI`Notebook`AppExtensions`",
"Notebook`Editor`",
"JerryI`WLX`WebUI`"
}]



Begin["`Private`"]

frontEndRuntime = <|
{"Modules", "js"} -> {},
{"Modules", "css"} -> {}
|>

rebuild := (
compiledString = {
StringRiffle[StringJoin["<script type=\"module\">", #, "</script>"] &/@ frontEndRuntime[{"Modules", "js"}] ],
StringRiffle[StringJoin["<style>", #, "</style>"] &/@ frontEndRuntime[{"Modules", "css"}] ]
} // StringRiffle;
);

rebuild;

component[__] := compiledString

AppExtensions`TemplateInjection["AppHead"] = component

injectInRuntime[{"Modules", "js"}, data_List] := With[{notebooks = Values[Notebook`HashMap]},
WebUISubmit[ Global`UIHeadInject["js", data ], #["Socket"] ] &/@ notebooks;
]

EventHandler[NotebookEditorChannel // EventClone,
{
"RequestRuntimeExtensions" -> Function[assoc,
With[{result = frontEndRuntime, kernel = Kernel`HashMap[assoc["Kernel"] ], promise = assoc["Promise"]},
Kernel`Async[kernel, EventFire[promise, Resolve, result] ];
]
],

"UpdateRuntimeExtensions" -> Function[assoc,
With[{promise = assoc["Promise"], data = assoc["Data"], kernel = Kernel`HashMap[assoc["Kernel"] ], key = assoc["Key"]},

With[{new = Complement[data, frontEndRuntime[key] // DeleteDuplicates ] // DeleteDuplicates},
frontEndRuntime[key] = data // DeleteDuplicates;
injectInRuntime[key, new];
];

rebuild;
Pause[0.3];
Kernel`Async[kernel, EventFire[promise, Resolve, True] ];
]
]

}
]

End[]
EndPackage[]
37 changes: 37 additions & 0 deletions src/FrontendRuntimeKernel.wl
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@


BeginPackage["Notebook`Editor`Kernel`FrontEndRuntime`", {
"JerryI`Misc`Events`",
"JerryI`Misc`Events`Promise`"
}]

FrontEndRuntime::usage = "FrontEndRuntime[] gives a list of properties possible to extend during the runtime"

Begin["`Internal`"]

FrontEndRuntime[any_List] := With[{r = FrontEndRuntime[]},
If[FailureQ[r],
$Failed
,
r[any]
]
]

FrontEndRuntime[] := With[{promise = Promise[]},
EventFire[Internal`Kernel`CommunicationChannel, "RequestRuntimeExtensions", <|"Promise" -> (promise), "Kernel"->Internal`Kernel`Hash|>];
With[{r = (promise // WaitAll)},
r
]
]

FrontEndRuntime /: Set[FrontEndRuntime[key_], data_List] := With[{promise = Promise[]},
With[{
list = Select[data /. {File[path_String] :> Import[path, "Text"]}, StringQ]
},
EventFire[Internal`Kernel`CommunicationChannel, "UpdateRuntimeExtensions", <|"Promise" -> (promise), "Kernel"->Internal`Kernel`Hash, "Data"->list, "Key"->key|>];
promise // WaitAll
]
]

End[]
EndPackage[]
26 changes: 26 additions & 0 deletions src/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,32 @@ core['Notebook`Editor`Rasterize`Internal`OverlayView'].Create = async (args, env
}, 1000);
}

core.UIHeadInject = async (args, env) => {
const type = await interpretate(args[0], env);
await core.UIHeadInject[type](args.slice(1), env);
}

core.UIHeadInject.js = async (args, env) => {
const data = await interpretate(args[0], env);

data.forEach((el) => {
const script = document.createElement('script');
script.type = "module";
script.textContent = el;
document.head.appendChild(script);
})
}

core.UIHeadInject.css = async (args, env) => {
const data = await interpretate(args[0], env);

data.forEach((el) => {
const style = document.createElement('style');
style.textContent = el;
document.head.appendChild(style);
})
}

core.SystemOpen = async (args, env) => {
const type = await interpretate(args[1], env);
await core.SystemOpen[type](args[0], env);
Expand Down

0 comments on commit 51e880e

Please sign in to comment.