javascript - 将 CKEditor 与 Aurelia 集成

标签 javascript ecmascript-6 aurelia

我正在开发一个 SPA,需要 WYSIWYG 编辑器。为此,我决定将 CKEditor 与 aurelia、aurelia-cli、npm 和 typescript 结合使用。

我已经安装了npm install ckeditor --save

还在我的 aurelia.json 文件中,我添加了 ckeditor npm 包作为依赖项。

"dependencies": [
      {
        "name": "ckeditor",
        "path": "../node_modules/ckeditor",
        "main": "ckeditor",
        "resources": [
          "config.js",
          "skins/moono-lisa/editor.css",
          "lang/en.js"
        ]
      }
]

我还将 ckeditor.d.ts 类型定义添加到了我的 custom_typings 文件夹中。 https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/ckeditor

此时,当我刷新页面时,出现以下错误:

任何帮助将不胜感激:)

Uncaught ReferenceError: CKEDITOR is not defined
    at vendor-bundle.js:36119
(anonymous) @ vendor-bundle.js:36119
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin aurelia-templating-binding.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin aurelia-templating-binding.
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin aurelia-templating-resources.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin aurelia-templating-resources.
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin aurelia-event-aggregator.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin aurelia-event-aggregator.
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin aurelia-history-browser.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin aurelia-history-browser.
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin aurelia-templating-router.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin aurelia-templating-router.
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin resources/index.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin resources/index.
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin aurelia-dialog.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin aurelia-dialog.
vendor-bundle.js:11929 DEBUG [aurelia] Loading plugin aurelia-testing.
vendor-bundle.js:11929 DEBUG [aurelia] Configured plugin aurelia-testing.
vendor-bundle.js:11929 DEBUG [templating] importing resources for aurelia-templating-resources/compose []
vendor-bundle.js:11929 DEBUG [templating] importing resources for aurelia-templating-router/router-view []
vendor-bundle.js:11929 DEBUG [templating] importing resources for resources/elements/label-input.html []
vendor-bundle.js:11929 DEBUG [templating] importing resources for resources/elements/label-select.html []
vendor-bundle.js:11939 INFO [aurelia] Aurelia Started
vendor-bundle.js:11929 DEBUG [templating] importing resources for app.html []
vendor-bundle.js:11929 DEBUG [templating] importing resources for resources/dialogs/wysiwyg-editor-dialog.html ["resources/elements/wysiwyg-editor"]
vendor-bundle.js:3790 GET http://localhost/viewer_editor/node_modules/ckeditor/ckeditor.js 
vendor-bundle.js:1399 Unhandled rejection Error: Script error for "ckeditor/ckeditor", needed by: resources/elements/wysiwyg-editor
http://requirejs.org/docs/errors.html#scripterror
    at F (http://localhost/app/263/scripts/vendor-bundle.js:3763:290)
    at HTMLScriptElement.onScriptError (http://localhost/app/263/scripts/vendor-bundle.js:3786:113)
From previous event:
    at DefaultLoader.loadModule (http://localhost/app/263/scripts/vendor-bundle.js:11807:14)
    at DefaultLoader.loadAllModules (http://localhost/app/263/scripts/vendor-bundle.js:11754:25)
    at ViewEngine.importViewResources (http://localhost/app/263/scripts/vendor-bundle.js:19396:26)
    at ViewEngine.loadTemplateResources (http://localhost/app/263/scripts/vendor-bundle.js:19366:19)
    at http://localhost/app/263/scripts/vendor-bundle.js:19314:41
From previous event:
    at ViewEngine.loadViewFactory (http://localhost/app/263/scripts/vendor-bundle.js:19298:67)
    at ConventionalViewStrategy.loadViewFactory (http://localhost/app/263/scripts/vendor-bundle.js:16718:25)
    at HtmlBehaviorResource.load (http://localhost/app/263/scripts/vendor-bundle.js:20056:29)
    at http://localhost/app/263/scripts/vendor-bundle.js:20589:18
From previous event:
    at CompositionEngine.createController (http://localhost/app/263/scripts/vendor-bundle.js:20577:71)
    at CompositionEngine._createControllerAndSwap (http://localhost/app/263/scripts/vendor-bundle.js:20556:19)
    at CompositionEngine.compose (http://localhost/app/263/scripts/vendor-bundle.js:20636:21)
    at http://localhost/app/scripts/app-bundle.js:4618:44
From previous event:
    at http://localhost/app/scripts/app-bundle.js:4613:122
From previous event:
    at _openDialog (http://localhost/app/scripts/app-bundle.js:4609:66)
    at http://localhost/app/scripts/app-bundle.js:4555:16
From previous event:
    at DialogService.open (http://localhost/app/scripts/app-bundle.js:4551:21)
    at CommonDialogs.showWysiwygEditorDialog (http://localhost/app/scripts/app-bundle.js:1375:39)
    at Scene.openWysiwygEditorDialog (http://localhost/app/scripts/app-bundle.js:3591:32)
    at CallScope.evaluate (http://localhost/app/263/scripts/vendor-bundle.js:6658:21)
    at Listener.callSource (http://localhost/app/263/scripts/vendor-bundle.js:10100:42)
    at http://localhost/app/263/scripts/vendor-bundle.js:10124:24
    at HTMLDocument.handleDelegatedEvent (http://localhost/app/263/scripts/vendor-bundle.js:8303:11)
printWarning @ vendor-bundle.js:1399
formatAndLogError @ vendor-bundle.js:1115
fireRejectionEvent @ vendor-bundle.js:1140
Promise._notifyUnhandledRejection @ vendor-bundle.js:587
(anonymous) @ vendor-bundle.js:132

最佳答案

您必须在代码中导入 CKEditor 的脚本。像这样:

import 'ckeditor';

这是生成 CKEditor 的自定义元素的示例。

editor.js

import {inject} from 'aurelia-dependency-injection';
import {bindable} from 'aurelia-templating';
import {bindingMode} from 'aurelia-binding';
import {DOM} from 'aurelia-pal';
import 'ckeditor';

@inject(DOM.Element)
export class Editor {
  @bindable({ defaultBindingMode: bindingMode.twoWay }) value;
  @bindable name;

  constructor(element) {
    this.element = element;
  }

  updateValue() {
    this.value = this.textArea.value;
  }

  bind() {
    this.textArea = this.element.getElementsByTagName('textarea')[0];
    let editor = CKEDITOR.replace(this.textArea);
    editor.on('change', (e) => {
      this.value = e.editor.getData();
    });
  }
}

editor.html

<template>
  <textarea change.delegate="updateValue()"></textarea>
  <input type="hidden" name.bind="name" value.bind="value">
</template>

要在生产环境中使用此功能,您必须导出一些 ckeditor 静态文件,config.js、styles.css、content.css 等。不幸的是,现在无法在 CLI 中执行此操作(我已经制作了一个 PR 来添加此功能 https://github.com/aurelia/cli/pull/415 )。因此,您必须在构建应用程序时创建一个 gulp 任务来导出这些文件。

关于javascript - 将 CKEditor 与 Aurelia 集成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41668170/

相关文章:

javascript - 绑定(bind) ref 在 vue.js 中不起作用?

typescript - Visual Studio Code - 在所有 ES6 导入中应用重命名文件重构

jquery - 在 Typescript 类中访问 jquery 验证变量

javascript - 为什么 ?函数的对象参数返回未定义的 JS

javascript - 无法从 CSV 下载 .json 文件到 JSON 转换并且无法将 JSON 转换为 CSV

javascript - Visual Studio 创建新的 Javascript 代码段

javascript - `eval` 调用上下文正在评估形参初始值设定项时声明实例化

javascript - 在 Aurelia 应用程序中使用 Google 的 reCAPTCHA

typescript - 将 TypeScript 目标更新为 ES6 时来自 aurelia-bundler 的 JS_Parse_Error

javascript - 如何根据 if else 使用 javascript 函数?