【发布时间】:2019-06-08 10:22:39
【问题描述】:
我想使用 CKEditor 并使用 Angular 7 拖放。他们在他们的网站上成功地做到了,但我似乎找不到任何 Angular 解决方案。正如你在这里看到的:
https://ckeditor.com/docs/ckeditor4/latest/examples/draganddrop.html
但我无法将其转换为 Angular 7 组件。我的代码是:
import { Component, OnInit } from '@angular/core';
import ClassicEditor from '@ckeditor/ckeditor5-build-classic';
@Component({
selector: 'xxxx',
templateUrl: 'xxxxx',
styleUrls: ['xxxxx']
})
export class NyttInnholdComponent implements OnInit {
public editor = ClassicEditor;
public contacts = [];
public model = {
editorData: '<p>Tommy says, Hello!</p>'
};
constructor() {
this.contacts = [{
name: 'Huckleberry Finn',
tel: '+48 1345 234 235',
email: 'h.finn@example.com',
avatar: 'hfin'
},
{
name: 'D\'Artagnan',
tel: '+45 2345 234 235',
email: 'dartagnan@example.com',
avatar: 'dartagnan'
},
{
name: 'Phileas Fogg',
tel: '+44 3345 234 235',
email: 'p.fogg@example.com',
avatar: 'pfog'
},
{
name: 'Alice',
tel: '+20 4345 234 235',
email: 'alice@example.com',
avatar: 'alice'
},
{
name: 'Little Red Riding Hood',
tel: '+45 2345 234 235',
email: 'lrrh@example.com',
avatar: 'lrrh'
}
];
}
ngOnInit() {
//this.teswt ();
this.editor.disableAutoInline = true;
this.editor.plugins.add('hcard', {
requires: 'widget',
init: function(editor) {
editor.widgets.add('hcard', {
allowedContent: 'span(!h-card); a[href](!u-email,!p-name); span(!p-tel)',
requiredContent: 'span(h-card)',
pathName: 'hcard',
upcast: function(el) {
return el.name == 'span' && el.hasClass('h-card');
}
});
// This feature does not have a button, so it needs to be registered manually.
editor.addFeature(editor.widgets.registered.hcard);
// Handle dropping a contact by transforming the contact object into HTML.
// Note: All pasted and dropped content is handled in one event - editor#paste.
editor.on('paste', function(evt) {
var contact = evt.data.dataTransfer.getData('contact');
if (!contact) {
return;
}
evt.data.dataValue =
'<span class="h-card">' +
'<a href="mailto:' + contact.email + '" class="p-name u-email">' + contact.name + '</a>' +
' ' +
'<span class="p-tel">' + contact.tel + '</span>' +
'</span>';
});
}
});
this.editor.on('instanceReady', function() {
// When an item in the contact list is dragged, copy its data into the drag and drop data transfer.
// This data is later read by the editor#paste listener in the hcard plugin defined above.
this.editor.document.getById('contactList').on('dragstart', function(evt) {
// The target may be some element inside the draggable div (e.g. the image), so get the div.h-card.
var target = evt.data.getTarget().getAscendant('div', true);
// Initialization of the CKEditor data transfer facade is a necessary step to extend and unify native
// browser capabilities. For instance, Internet Explorer does not support any other data type than 'text' and 'URL'.
// Note: evt is an instance of CKEDITOR.dom.event, not a native event.
this.editor.plugins.clipboard.initDragDataTransfer(evt);
var dataTransfer = evt.data.dataTransfer;
// Pass an object with contact details. Based on it, the editor#paste listener in the hcard plugin
// will create the HTML code to be inserted into the editor. You could set 'text/html' here as well, but:
// * It is a more elegant and logical solution that this logic is kept in the hcard plugin.
// * You do not know now where the content will be dropped and the HTML to be inserted
// might vary depending on the drop target.
dataTransfer.setData('contact', this.contacts[target.data('contact')]);
// You need to set some normal data types to backup values for two reasons:
// * In some browsers this is necessary to enable drag and drop into text in the editor.
// * The content may be dropped in another place than the editor.
dataTransfer.setData('text/html', target.getText());
// You can still access and use the native dataTransfer - e.g. to set the drag image.
// Note: IEs do not support this method... :(.
if (dataTransfer.$.setDragImage) {
dataTransfer.$.setDragImage(target.findOne('img').$, 0, 0);
}
});
});
// Initialize the editor with the hcard plugin.
this.editor.inline('editor1', {
extraPlugins: 'hcard,sourcedialog,justify'
});
}
}
这样使用:
<ckeditor [(ngModel)]="model.editorData" [editor]="editor"></ckeditor>
如果我尝试这样做,this.editor 将不会包含我期望的任何属性。您可以在这里找到:https://ckeditor.com/docs/ckeditor5/latest/api/module_editor-classic_classiceditor-ClassicEditor.html
那么,有谁知道我如何让 Angular 与 Angular 7 和 CKEditor 一起工作?
【问题讨论】:
-
不确定使用 cdk-drag-and-drop 是否真的可行/实用。默认情况下,您将项目放入 cdkDropLists,但 cdkDropLists 看起来与此编辑器完全不同。我会复制他们使用的相同方法。记住“javascript 是有效的打字稿”。
-
请注意,您使用 CKEditor 5 以及 CKEditor 4 的插件和 API。这将不起作用。
-
嗯,我知道了……现在。但当时没有;)。我只需要通过 CDN 调用 CKEditor 4 并在控制器中进行一些小的更改以使其工作。太糟糕了,他们没有合适的 Angular 2+ 拖放解决方案。现在它是一个“黑客”。
标签: javascript angular drag-and-drop ckeditor angular7