ckeditor4.x - 如何在 stenciljs 中添加 npm ckeditor4?
问题描述
我已经将 npm i ckeditor4 安装到我的模板项目中,并且我已经像这样使用它。但我没有得到ckeditor,告诉我在哪里添加脚本标签我对模具完全陌生
ui-editor.tsx
import { Component, h } from '@stencil/core';
@Component({
tag: 'ui-editor',
styleUrl: 'style.scss',
shadow: true
})
export class UiEditor {
render() {
return (
<div id="editor">
<p>This is the editor content.</p>
</div>
)
}
}
正如文档https://www.npmjs.com/package/ckeditor4中所说,我应该在哪里添加脚本
<script src="./node_modules/ckeditor4/ckeditor.js"></script>
<script>
CKEDITOR.replace( 'editor' );
</script>
解决方案
尝试从 index.html 文件中删除脚本标记。以下组件将自动从 unpkg 添加脚本标签。
import { h, Component, State, Host } from "@stencil/core";
@Component({
tag: "ck-editor"
})
export class CkEditor {
_textarea: HTMLTextAreaElement;
componentWillLoad() {
return this.appendScript();
}
componentDidLoad() {
//@ts-ignore
let editor = CKEDITOR.replace(this._textarea, {
width: "99%",
height: "300px",
});
}
private async submit() {
// @ts-ignore
console.log(
CKEDITOR.instances[
this._textarea.nextSibling.id.replace("cke_", "")
].getData()
);
}
appendScript() {
return new Promise((resolve) => {
if (document.getElementById("ckeditor-script")) {
resolve();
return;
}
const ckeditorScript = document.createElement("script");
ckeditorScript.id = "ckeditor-script";
ckeditorScript.src = "https://unpkg.com/ckeditor4@4.14.1/ckeditor.js";
ckeditorScript.onload = () => resolve();
document.body.appendChild(ckeditorScript);
});
}
render() {
return (
<Host>
<textarea ref={(el) => (this._textarea = el)}></textarea>
<button onClick={() => this.submit()}>Submit</button>
</Host>
);
}
}
您应该能够导入 ckeditor 但我还没有测试过它在汇总中的处理方式。我最近从事的项目已经从 unpkg 加载 ckeditor,所以我们改为朝那个方向发展。
推荐阅读
- javascript - 根据另一个对象数组修改一个对象数组
- javascript - 动态页面不会更改呈现的组件,但单击后退按钮时 URL 会更改,使用链接
- sql-server - MS SQL 将管理员冒充为“访客”
- python - Deeplab:通过运行“sh convert_cityscapes.sh”将数据集转换为 TFRecord 时出错
- javascript - 将本地文件中的数据加载到 Excel 加载项
- python - 在 Python 中优化数据帧子集操作
- python - 从python列表中获取特定数字的最佳方法是什么?
- javascript - 如何从 React 中的购物车中删除两个冲突的 id?
- python - 现在 tf.keras.backend 有什么用,是否更安全/更面向未来进行编码?
- spring-batch - Spring批处理根据读取的行动态更改块大小