mirror of
https://github.com/zhw2590582/ArtPlayer.git
synced 2026-10-29 19:41:25 -08:00
refactor(docs): [SITE-03] migrate desktop editor and integrate typed generation
This commit is contained in:
1 parent
a468cb2c39
commit
2f130c58f1
37 files changed
+1921
-305
No files matched your search
@@ -0,0 +1,51 @@
|
||||
interface SessionOptions {
|
||||
loadLibraries: (encoded: string) => Promise<unknown>
|
||||
exampleSource: (name: string) => Promise<string>
|
||||
writeCode: (source: string) => void
|
||||
compile: (source: string) => Promise<string>
|
||||
execute: (code: string) => void
|
||||
}
|
||||
|
||||
export function createEditorSession(options: SessionOptions) {
|
||||
let generation = 0
|
||||
let disposed = false
|
||||
return {
|
||||
async run({
|
||||
libs = '',
|
||||
code,
|
||||
example,
|
||||
}: {
|
||||
libs?: string
|
||||
code?: string
|
||||
example?: string
|
||||
}): Promise<void> {
|
||||
const current = ++generation
|
||||
const active = () => !disposed && current === generation
|
||||
if (!active())
|
||||
return
|
||||
await options.loadLibraries(libs)
|
||||
if (!active())
|
||||
return
|
||||
const source = example
|
||||
? await options.exampleSource(example)
|
||||
: code
|
||||
? decodeURIComponent(code).trim()
|
||||
: await options.exampleSource('index')
|
||||
if (!active())
|
||||
return
|
||||
options.writeCode(source)
|
||||
const executable = await options.compile(source)
|
||||
if (active())
|
||||
options.execute(executable)
|
||||
},
|
||||
dispose(): void {
|
||||
disposed = true
|
||||
generation++
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function executeClassic(code: string): void {
|
||||
// eslint-disable-next-line no-new-func -- The existing editor explicitly executes user-entered classic script on Run.
|
||||
new Function(code)()
|
||||
}
|
||||
Reference in new issue
Block a user