feat(iframe): 添加通过URL参数filePath自动加载文件功能
新增demo页面展示iframe加载功能 添加测试数据文件public/test.ocd
This commit is contained in:
1 parent
e78b66bb95
commit
41313ac835
3 files changed
+638
No files matched your search
+28
@@ -66,6 +66,34 @@ window.onload = function () {
|
||||
// cypress使用
|
||||
Reflect.set(window, 'editor', instance)
|
||||
|
||||
// 通过 URL 参数 filePath 自动打开同源文件(iframe 场景)
|
||||
const params = new URLSearchParams(window.location.search)
|
||||
const filePath = params.get('filePath')
|
||||
if (filePath) {
|
||||
const url = new URL(filePath, window.location.href).toString()
|
||||
fetch(url)
|
||||
.then(res => res.text())
|
||||
.then(text => {
|
||||
try {
|
||||
let editorData
|
||||
try {
|
||||
editorData = JSON.parse(text)
|
||||
} catch {
|
||||
editorData = { main: [{ value: text }] }
|
||||
}
|
||||
instance.command.executeSetValue(editorData)
|
||||
console.log('[iframe] 根据 filePath 加载完成:', url)
|
||||
} catch (err) {
|
||||
console.error('[iframe] 加载失败:', err)
|
||||
alert('通过参数加载文件失败,请检查格式与同源权限')
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('[iframe] 获取文件失败:', err)
|
||||
alert('无法获取 filePath 指定的文件')
|
||||
})
|
||||
}
|
||||
|
||||
// 菜单弹窗销毁
|
||||
window.addEventListener(
|
||||
'click',
|
||||
|
||||
Reference in new issue
Block a user