2016-09-21 23 views
1

我想使用CKEditor和我的Ember應用程序。 我是Ember的100%n00b,但我到了那裏。Ember中的Ckeditor使用

我已經盡darndest摸不着頭腦,但我已經得到無處:(

我曾嘗試使用ember-ckeditor,這結束了與編輯扔了一堆net::ERR_NAME_NOT_RESOLVED錯誤的東西,如配置.js文件和其他「資產」,預期在資產文件夾中找到。

我已經試過ember-cli-ckeditor。完全相同的問題如上。

這兩個插件具有相當蹩腳的文檔。例如,我不知道如何提供自定義配置文件,CSS等。或者如果我w螞蟻使用CkFinder?

上述兩個插件還拋出一些加載了服務器時貶值的警告,但我disgress ....

我終於嘗試手動包括在vendor文件夾CKEditor的v4.5.6。 然後我包含在ember-cli-build.js之中:app.import('vendor/ckeditor/ckeditor.js'); 我不知道我是否正確執行此操作,如果是這樣,如何在我的控制器或組件中使用編輯器插件?

CKEDITOR.replace("content");按照Ember之外的常規方式嗎?

請上學吧!

回答

3

要使用無插件的CKEditor(創建自己的組件):

  1. 使用亭子安裝的CKEditor:

    bower install ckeditor --save 
    
  2. 安裝broccoli-funnel,你會需要它的CKEditor資產:

    npm install broccoli-funnel --save-dev 
    
  3. 在你燼-CLI-build.js:

    在頂部的文件請求漏斗

    var Funnel = require('broccoli-funnel'); 
    

    在app的選項ex從指紋CLUDE CKEditor的資產:

    var app = new EmberApp(defaults, { 
        fingerprint: { 
        exclude: ['assets/ckeditor/'] 
        } 
    }); 
    

    進口的CKEditor的JS和資產:

    app.import('bower_components/ckeditor/ckeditor.js'); 
    
    var ckeditorAssets = new Funnel('bower_components/ckeditor', { 
        srcDir: '/', 
        destDir: '/assets/ckeditor' 
    }); 
    
    /** 
    * If you need to use custom skin, put it into 
    * vendor/ckeditor/skins/<skin_name> 
    * Also, custom plugins may be added in this way 
    * (look ckeditor's info for details) 
    * If you don't need custom skins, you may remove 
    * ckeditorCustoms 
    */ 
    var ckeditorCustoms = new Funnel('vendor/ckeditor', { 
        srcDir: '/', 
        destDir: '/assets/ckeditor' 
    }); 
    
    return app.toTree([ckeditorAssets, ckeditorCustoms]); 
    
  4. 如果您的應用程序是不是在網站的根,你可能需要把這個腳本在指數的主體部分。在其他腳本之前:

    <script type="text/javascript"> 
        window.CKEDITOR_BASEPATH = '/path-to/assets/ckeditor/'; 
    </script> 
    
  5. 創建組件。 警告:這是來自我廢棄的寵物項目的代碼,我99%確定它不會爲你「按原樣」工作,因爲缺少依賴關係,並且因爲它是爲不同的html佈局創建的。但我認爲它可能有幫助。如果你想嘗試和複製粘貼,這裏有依賴關係:

    npm install --save-dev ember-browserify 
    npm install --save-dev sanitize-html 
    

    組件的代碼:

    /* globals CKEDITOR */ 
    import Ember from 'ember'; 
    import layout from '../templates/components/md-ckeditor'; //component's name! 
    import SanitizeHTML from 'npm:sanitize-html'; 
    
    export default Ember.Component.extend({ 
        layout: layout, 
        classNames: ['input-field'], 
    
        _editor: null, 
    
        bindAttributes: ['disabled', 'readonly', 'autofocus'], 
        validate: false, 
    
        errorsPath: 'errors', 
    
        init() { 
        this._super(...arguments); 
        const propertyPath = this.get('valueBinding._label'); 
        if (Ember.isPresent(propertyPath)) { 
         Ember.Binding.from(`targetObject.${this.get('errorsPath')}.${propertyPath}`) 
         .to('errors') 
         .connect(this); 
        } 
        }, 
    
        didInsertElement() { 
        var i18n = this.get('i18n'); 
    
        if (Ember.isPresent(this.get('icon'))) { 
         this.$('> span').css('padding-left', '3rem'); 
        } 
    
        this._setupLabel(); 
    
        this._editor = CKEDITOR.inline(this.element.querySelector('.ckeditor'), { 
         skin: 'minimalist', 
         toolbar: [ 
         ['Cut', 'Copy', 'Paste', 'PasteText', 'PasteFromWord'], 
         ['Undo', 'Redo'], 
         ['Bold', 'Italic', 'Strike'], 
         ['Link', 'Unlink'], 
         ['NumberedList', 'BulletedList', 'Blockquote'], 
         ['Source'] 
         ], 
         linkShowAdvancedTab: false, 
         linkShowTargetTab: false, 
         language: i18n.get('locale'), 
         removePlugins: 'elementspath' 
        }); 
        this._editor.on('instanceReady', (e) => { 
         this._updateValidClass(); 
        }); 
        this._editor.on('change', (e) => { 
         this.set('value', e.editor.getData()); 
        }); 
        this._editor.on('focus', (e) => { 
         var label = this.$('> label, > i'); 
         label.addClass('active'); 
        }); 
        this._editor.on('blur', (e) => { 
         var label = this.$('> label, > i'); 
         var text = SanitizeHTML(e.editor.getData(), { 
         allowedTags: [] 
         }).trim(); 
         if (text !== '') { 
         label.addClass('active'); 
         } else { 
         label.removeClass('active'); 
         } 
        }); 
        }, 
    
        willDestroyElement() 
        { 
        this._editor.destroy(); 
        this._editor = null; 
        },   
    
        id: Ember.computed('elementId', function() { 
        return `${this.get('elementId')}-input`; 
        }), 
    
        validClass: Ember.computed('value', 'errors', function() { 
        var errors = this.get('errors'); 
        if (errors && errors.get && errors.get('firstObject')) { 
         return 'invalid'; 
        } else if (!!this.get('value')) { 
         return 'valid'; 
        } else { 
         return ''; 
        } 
        }), 
    
        validClassChanged: Ember.observer('validClass', function() { 
        Ember.run.once(this, '_updateValidClass'); 
        }), 
    
        _updateValidClass() { 
        if (this._editor && this._editor.container &&   this._editor.container.$) { 
         Ember.$(this._editor.container.$).removeClass('invalid   valid').addClass(this.get('validClass')); 
        } 
        }, 
    
        _setupLabel() { 
        const label = this.$('> label, > i'); 
        if (Ember.isPresent(this.get('value'))) { 
         label.addClass('active'); 
        } 
        } 
    }); 
    

    模板:

    {{textarea 
    id=id 
    value=value 
    name=name 
    required=required 
    readonly=readonly 
    disabled=disabled 
    maxlength=maxlength 
    class="materialize-textarea ckeditor" 
    classNameBindings="validate:validate: validClass" 
    }} 
    <label for="{{id}}">{{label}}</label> 
    <small class="red-text"> 
        {{#if errors}} {{errors.firstObject}} {{else}} &nbsp; {{/if}} 
    </small>