如何在grapesjs中为js代码添加选项?

huangapple go评论92阅读模式
英文:

How to add an option for js code in grapesjs?

问题

现在我想要导出我的JS代码?应该如何做?
我注意到在块管理器中,如果我们将内容编辑如下:

editor.BlockManager.add("sample input field", {
  label: "input-field",
  category: "sample",
  content: {
    script: "我的脚本"
  }
});

脚本将嵌入到我的HTML文件中,但如果我将其编辑为以下内容:

content:`一些HTML代码
<script type="text/javascript" myscript></script>`

我无法在我的HTML文件中获取脚本内容....如何将脚本嵌入到内容中?
为什么它没有显示在提取的HTML文件中?

英文:

如何在grapesjs中为js代码添加选项?

Now I want my JS code also to be exported? How is this done?
I have seen that in the block manager if we edit the content as follows

editor.BlockManager.add("sample input field", {
  label: "input-field",
  category: "sample",
  content: {
            script:"my script"
           }
});

the script is embedded in my HTML file but if I edit it as follows

content:` some HTML code
<script type="text/javascript" myscript></script>`

I do not get the script content in my HTML file....how to get the script embedded in the content?
Why is it not being displayed in the extracted HTML file?

答案1

得分: 4

你需要在初始化你的grapesjs编辑器时使用allowScripts配置选项。

const editor = grapesjs.init({
    ... // 其余的grapesjs配置
    allowScripts: 1,
});

默认情况下脚本是禁用的,但这个选项会启用它们。

英文:

You need to use the allowScripts config option when initializing your grapesjs editor.

const editor = grapesjs.init({
    ... // the rest of your grapesjs config
    allowScripts: 1,
});

Scripts are disabled by default, but this option turns them on.

答案2

得分: 0

创建一个自定义代码。您可以查看默认视图代码,在那里有一个名为buildEditor的函数,用于完成所有配置。还需要传递HTML、CSS和JS。

const oHtmlEd = buildEditor('htmlmixed', 'hopscotch', 'HTML', editor);
const oCsslEd = buildEditor('css', 'hopscotch', 'CSS', editor);
const oJSlEd = buildEditor('js', 'hopscotch', 'JS', editor);
英文:

Create a custom code. You can check the default view code, in that buildEditor function is there where all config is done. Along with HTML, CSS, pass Js also

const oHtmlEd = buildEditor('htmlmixed', 'hopscotch', 'HTML', editor);
const oCsslEd = buildEditor('css', 'hopscotch', 'CSS', editor);
const oJSlEd = buildEditor('js', 'hopscotch', 'JS', editor);

答案3

得分: 0

const editor = grapesjs.init({
  canvas: {
    scripts: ['https://.../somelib.min.js'],
    // The same would be for external styles
    styles: ['https://.../ext-style.min.css'],
  }
});

来源

英文:
const editor = grapesjs.init({
  canvas: {
        scripts: ['https://.../somelib.min.js'],
        // The same would be for external styles
        styles: ['https://.../ext-style.min.css'],
       }
   });

Source

huangapple
  • 本文由 发表于 2020年1月3日 15:25:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定