-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from wpdas/feature/new-importables
Feature: new importable files
- Loading branch information
Showing
20 changed files
with
397 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { createContext } from "../alem-vm"; | ||
|
||
const ModulesContext = () => { | ||
const { setDefaultData, updateData, getSelf } = | ||
createContext<any>("alemModulesContext"); | ||
|
||
setDefaultData({ | ||
calls: {}, | ||
|
||
// | ||
callModule: ( | ||
setupCode: string, | ||
code: string, | ||
callId: number, | ||
onComplete: (data: any) => void, | ||
) => { | ||
if (!code || !callId) return; | ||
|
||
const codeStructure = ` | ||
${setupCode} | ||
event.source.postMessage({response: ${code.replaceAll(";", "")}, forCallId: ${callId}}, "*"); | ||
`; | ||
|
||
const updatedCalls = { ...getSelf().calls }; | ||
|
||
// Registra as chamadas | ||
updatedCalls[callId] = { code: codeStructure, handler: onComplete }; | ||
|
||
updateData({ | ||
calls: updatedCalls, | ||
}); | ||
}, | ||
|
||
removeCall: (callId: number) => { | ||
const updatedCalls: any = {}; | ||
const currentCalls = getSelf().calls; | ||
const calls = Object.keys(currentCalls); | ||
calls.forEach((call) => { | ||
if (call !== callId.toString()) { | ||
updatedCalls[call] = currentCalls[call]; | ||
} | ||
}); | ||
|
||
updateData({ | ||
calls: updatedCalls, | ||
}); | ||
}, | ||
}); | ||
}; | ||
|
||
export default ModulesContext; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import ModulesContext from "./ModulesContext"; | ||
import { ChildrenProps, ModuleResponseData, useContext } from "../alem-vm"; | ||
|
||
const ModulesProvider = ({ children }: ChildrenProps) => { | ||
ModulesContext(); | ||
|
||
const modulesHandler = ` | ||
<script>:::MODULES_SRC:::</script> | ||
<script> | ||
window.addEventListener("message", (event) => { | ||
if (event.data.code) { | ||
eval(event.data.code); | ||
} | ||
}, false); | ||
</script> | ||
`; | ||
|
||
const modules = useContext<any>("alemModulesContext"); | ||
const calls = modules.calls; | ||
const callsKeys = Object.keys(calls); | ||
|
||
return ( | ||
<> | ||
{callsKeys.map((callKey) => ( | ||
<iframe | ||
style={{ height: 0, width: 0 }} | ||
srcDoc={modulesHandler} | ||
message={{ | ||
code: calls[callKey].code, | ||
}} | ||
onMessage={(message: ModuleResponseData) => { | ||
if (message) { | ||
calls[message.forCallId].handler(message.response); | ||
modules.removeCall(message.forCallId); | ||
} | ||
}} | ||
/> | ||
))} | ||
{children} | ||
</> | ||
); | ||
}; | ||
|
||
export default ModulesProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.