Skip to content

Commit 9f0aef6

Browse files
committed
fixed script module for multiple frames
1 parent b5597cd commit 9f0aef6

File tree

3 files changed

+41
-27
lines changed

3 files changed

+41
-27
lines changed

src/core/MountScene.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const MountScene = (scene) => {
99
const child = sceneConfig._el
1010
const projectConfig = sceneConfig._projectConfig
1111

12+
let modInstances = []
13+
const modPromises = []
14+
1215
const initModules = (runBefore) => {
1316
if (sceneConfig.modules) {
1417
for (const k in sceneConfig.modules) {
@@ -19,7 +22,7 @@ const MountScene = (scene) => {
1922
if (modConfig) {
2023
if (!Mod.runBefore && !runBefore) {
2124
// eslint-disable-next-line
22-
new Mod(child, modConfig, sceneConfig)
25+
modPromises.push(new Mod(child, modConfig, sceneConfig))
2326
}
2427
}
2528
}
@@ -39,13 +42,16 @@ const MountScene = (scene) => {
3942
}, projectConfig._transitionDestroyDelay)
4043
}
4144

45+
sceneConfig.blocks.forEach(b => MountBlock(b))
4246
initModules(false)
4347

44-
sceneConfig.blocks.forEach(b => MountBlock(b))
48+
Promise.all(modPromises).then(data => {
49+
modInstances = data
4550

46-
startTransition()
51+
startTransition()
4752

48-
child.classList.add('presentaSceneMounted')
53+
child.classList.add('presentaSceneMounted')
54+
})
4955
}
5056

5157
export { MountScene }

src/core/Scene.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const Scene = function (cont, sceneConfig, projectConfig, rootElement) {
99
return new Promise(function (resolve, reject) {
1010
let blockInstances = []
1111

12-
const modInstances = []
12+
let modInstances = []
1313
const blockPromises = []
1414
const preModPromises = []
1515

@@ -163,6 +163,7 @@ const Scene = function (cont, sceneConfig, projectConfig, rootElement) {
163163
initModules(true)
164164

165165
Promise.all(preModPromises).then(data => {
166+
modInstances = data
166167
initBlocks()
167168

168169
Promise.all(blockPromises).then(data => {

src/modules/script/index.js

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
const appendScriptTag = (url, code) => {
1+
2+
const appendScriptTag = (url, code, id) => {
23
const ns = document.createElement('script')
3-
ns.setAttribute('class', 'sdpmodulescriptcontainer')
4+
ns.setAttribute('class', 'sdpmodulescriptcontainer' + id)
45
ns.setAttribute('type', 'module')
56
ns.setAttribute('async', '')
67
ns.innerHTML = code
@@ -9,41 +10,47 @@ const appendScriptTag = (url, code) => {
910
}
1011

1112
const script = function (element, mod, config) {
12-
if (config._mode === 'preview') return
13+
if (!mod.forceRun && config._mode === 'preview') return
1314
if (config.contextType !== 'scene') return
1415

16+
const id = '_JSMOD_' + parseInt(Math.random() * 10000)
17+
const that = this
18+
19+
this.destroy = () => {
20+
const prev = [...document.querySelectorAll('.sdpmodulescriptcontainer' + id)]
21+
prev.forEach(d => document.body.removeChild(d))
22+
}
23+
1524
return new Promise((resolve, reject) => {
1625
const blink = {}
1726
config.blocks.forEach(b => {
1827
blink[b.ukey] = b
1928
})
2029
blink._otherParams = config.otherParams
21-
window._sdpconfigobject = blink
30+
window['_sdpconfigobject' + id] = blink
2231

23-
window._sdpscriptexportedresult = {}
32+
window['_sdpscriptexportedresult' + id] = {}
2433

25-
window._sdpcallbackfunc = () => {
26-
console.log('_sdpcallbackfunc')
27-
window._sdpcallbackfunc = null
28-
window._sdpconfigobject = null
34+
window['_sdpcallbackfunc' + id] = () => {
35+
console.log('_sdpcallbackfunc' + id)
36+
window['_sdpcallbackfunc' + id] = null
37+
window['_sdpconfigobject' + id] = null
2938

30-
resolve()
39+
resolve(that)
3140
}
3241

33-
// remove previous code modules
34-
const prev = [...document.querySelectorAll('.sdpmodulescriptcontainer')]
35-
prev.forEach(d => document.body.removeChild(d))
36-
3742
let code = `
3843
const index = ${config.index}
39-
const exportedResult = window._sdpscriptexportedresult
44+
const exportedResult = window._sdpscriptexportedresult${id}
4045
41-
const params = window._sdpconfigobject._otherParams
46+
const params = window._sdpconfigobject${id}._otherParams
4247
`
4348
config.blocks.forEach(b => {
44-
code += `
45-
const ${b.ukey} = window._sdpconfigobject.${b.ukey}
49+
if (b.ukey) {
50+
code += `
51+
const ${b.ukey} = window._sdpconfigobject${id}.${b.ukey}
4652
`
53+
}
4754
})
4855

4956
code += `
@@ -55,22 +62,22 @@ try{
5562
${mod.code}
5663
5764
}catch(err){
58-
console.log('error in module', err)
65+
console.log('error in module', err)
5966
}
6067
export default {}`
6168

6269
// add the code module
6370
let url = URL.createObjectURL(new Blob([code], { type: 'application/javascript' }))
64-
appendScriptTag(url, code)
71+
appendScriptTag(url, code, id)
6572

6673
// add the last module for callback
6774
const lastModule = `
6875
import _sdpPrivateInput from '${url}'
69-
window._sdpcallbackfunc()
76+
window._sdpcallbackfunc${id}()
7077
`
7178

7279
url = URL.createObjectURL(new Blob([lastModule], { type: 'application/javascript' }))
73-
appendScriptTag(url, lastModule)
80+
appendScriptTag(url, lastModule, id)
7481
})
7582
}
7683

0 commit comments

Comments
 (0)