-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathplugin.js
78 lines (70 loc) · 3.2 KB
/
plugin.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Copyright (c) 2014-2025, CKSource Holding sp. z o.o. All rights reserved.
* Licensed under the terms of the MIT License (see LICENSE.md).
*
* Simple CKEditor Widget (Part 1).
*
* Created out of the CKEditor Widget SDK:
* https://ckeditor.com/docs/ckeditor4/latest/guide/widget_sdk_tutorial_1.html
*/
// Register the plugin within the editor.
CKEDITOR.plugins.add( 'simplebox', {
// This plugin requires the Widgets System defined in the 'widget' plugin.
requires: 'widget',
// Register the icon used for the toolbar button. It must be the same
// as the name of the widget.
icons: 'simplebox',
// The plugin initialization logic goes inside this method.
init: function( editor ) {
// Register the simplebox widget.
editor.widgets.add( 'simplebox', {
// Allow all HTML elements and classes that this widget requires.
// Read more about the Advanced Content Filter here:
// * https://ckeditor.com/docs/ckeditor4/latest/guide/dev_advanced_content_filter.html
// * https://ckeditor.com/docs/ckeditor4/latest/guide/plugin_sdk_integration_with_acf.html
allowedContent: 'div(!simplebox); div(!simplebox-content); h2(!simplebox-title)',
// Minimum HTML which is required by this widget to work.
requiredContent: 'div(simplebox)',
// Define two nested editable areas.
editables: {
title: {
// Define a CSS selector used for finding the element inside the widget element.
selector: '.simplebox-title',
// Define content allowed in this nested editable. Its content will be
// filtered accordingly and the toolbar will be adjusted when this editable
// is focused.
allowedContent: 'br strong em'
},
content: {
selector: '.simplebox-content',
allowedContent: 'p br ul ol li strong em'
}
},
// Define the template of a new Simple Box widget.
// The template will be used when creating new instances of the Simple Box widget.
template:
'<div class="simplebox">' +
'<h2 class="simplebox-title">Title</h2>' +
'<div class="simplebox-content"><p>Content...</p></div>' +
'</div>',
// Define the label for a widget toolbar button which will be automatically
// created by the Widgets System. This button will insert a new widget instance
// created from the template defined above, or will edit selected widget
// (see second part of this tutorial to learn about editing widgets).
//
// Note: In order to be able to translate your widget you should use the
// editor.lang.simplebox.* property. A string was used directly here to simplify this tutorial.
button: 'Create a simple box',
// Check the elements that need to be converted to widgets.
//
// Note: The "element" argument is an instance of https://ckeditor.com/docs/ckeditor4/latest/api/CKEDITOR_htmlParser_element.html
// so it is not a real DOM element yet. This is caused by the fact that upcasting is performed
// during data processing which is done on DOM represented by JavaScript objects.
upcast: function( element ) {
// Return "true" (that element needs to converted to a Simple Box widget)
// for all <div> elements with a "simplebox" class.
return element.name == 'div' && element.hasClass( 'simplebox' );
}
} );
}
} );