ckeditor5 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23e478e9988398f0612c2831798b5cd7a2abb9e6f21a8777e22964fe97974041
4
- data.tar.gz: 817dfd3d0a94ef227c8e5845636468cf2aec54dbd19127f09feccd2c65d818cd
3
+ metadata.gz: e8e40a31c033d3d5cfc96667757993fd4a92614a5e5bedacb93ec20d82419bd2
4
+ data.tar.gz: fb172fb186502165dd06475a980fd9ccbd21751076cf5a420edc64ace4129734
5
5
  SHA512:
6
- metadata.gz: 00d576237488fcf65f5ceb1df860f1a2c6a31beccfc4361d57253ae014998072f083f282f66d4d0114e5eb8f50f3c838c56e172ded14bd559dbfe2b6d7c0abf4
7
- data.tar.gz: a76c84be02f3c76acd8c4a39505264677ab34e3b4536887e1309da41417846346cee2f86718fef8e64f897acd4f5488f373def7c2299a49ca5ac8397ae296cf9
6
+ metadata.gz: 64889f53d365c640f498efc484fbab12e05cfe93b80a9ab448c5bc5e3cb45bd24f313bce90ebd04cfd656c7cbd7c9cc60f7c982d7cf0aee8128cbc50633304ac
7
+ data.tar.gz: 1f9985d8cf29068eb70ecb93bf2c8e85a177750d0241e28075b1ba916a6e3a43cef71bd5598f4f25e25c6d9751cc36639065aa28065d592ff888012dbfd5bde3
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # CKEditor 5 Rails Integration
1
+ # CKEditor 5 Rails Integration
2
2
 
3
3
  [![License: MIT](https://img.shields.io/badge/License-MIT-orange.svg?style=flat-square)](https://opensource.org/licenses/MIT)
4
4
  ![Gem Version](https://img.shields.io/gem/v/ckeditor5?style=flat-square)
@@ -8,7 +8,7 @@
8
8
 
9
9
  Unofficial CKEditor 5 Ruby on Rails integration gem. Provides seamless integration of CKEditor 5 with Rails applications through web components and helper methods.
10
10
 
11
- ## Installation
11
+ ## Installation 🛠️
12
12
 
13
13
  Add this line to your application's Gemfile:
14
14
 
@@ -16,7 +16,49 @@ Add this line to your application's Gemfile:
16
16
  gem 'ckeditor5'
17
17
  ```
18
18
 
19
- ## Presets
19
+ Usage in your Rails application:
20
+
21
+ ```erb
22
+ <!-- app/views/demos/index.html.erb -->
23
+
24
+ <% content_for :head do %>
25
+ <%= ckeditor5_assets version: '43.2.0', translations: [:pl, :es] %>
26
+ <% end %>
27
+
28
+ <%= ckeditor5_editor style: 'width: 600px' %>
29
+ ```
30
+
31
+ Effect:
32
+
33
+ ![CKEditor 5 Classic Editor in Ruby on Rails application](docs/intro-classic-editor.png)
34
+
35
+ ## Table of Contents 📚
36
+
37
+ - [CKEditor 5 Rails Integration ✨](#ckeditor-5-rails-integration-)
38
+ - [Installation 🛠️](#installation-️)
39
+ - [Table of Contents 📚](#table-of-contents-)
40
+ - [Presets 🎨](#presets-)
41
+ - [Available Configuration Methods ⚙️](#available-configuration-methods-️)
42
+ - [`shape(type)` method](#shapetype-method)
43
+ - [`plugins(*names)` method](#pluginsnames-method)
44
+ - [`toolbar(*items, should_group_when_full: true)` method](#toolbaritems-should_group_when_full-true-method)
45
+ - [`menubar(visible: true)` method](#menubarvisible-true-method)
46
+ - [`language(ui, content:)` method](#languageui-content-method)
47
+ - [`configure(name, value)` method](#configurename-value-method)
48
+ - [`plugin(name, premium:, import_name:)` method](#pluginname-premium-import_name-method)
49
+ - [Including CKEditor 5 assets 📦](#including-ckeditor-5-assets-)
50
+ - [Lazy loading 🚀](#lazy-loading-)
51
+ - [GPL usage 🆓](#gpl-usage-)
52
+ - [Commercial usage 💰](#commercial-usage-)
53
+ - [Editor placement 🏗️](#editor-placement-️)
54
+ - [Classic editor 📝](#classic-editor-)
55
+ - [Multiroot editor 🌳](#multiroot-editor-)
56
+ - [Inline editor 📝](#inline-editor-)
57
+ - [Balloon editor 🎈](#balloon-editor-)
58
+ - [Decoupled editor 🌐](#decoupled-editor-)
59
+ - [License 📜](#license-)
60
+
61
+ ## Presets 🎨
20
62
 
21
63
  Presets are predefined configurations of CKEditor 5, allowing quick setup with specific features. The gem includes a `:default` preset with common features like bold, italic, underline, and link for the classic editor.
22
64
 
@@ -65,20 +107,26 @@ end
65
107
 
66
108
  You can generate your preset using the CKEditor 5 [online builder](https://ckeditor.com/ckeditor-5/online-builder/). After generating the configuration, you can copy it to the `config/initializers/ckeditor5.rb` file.
67
109
 
68
- ### Available Configuration Methods
110
+ ### Available Configuration Methods ⚙️
111
+
112
+ <details>
113
+ <summary>Expand to show available methods 📖</summary>
69
114
 
70
115
  #### `shape(type)` method
71
116
 
72
117
  Defines the type of editor. Available options:
73
118
 
74
- - `:classic` - standard editor
119
+ - `:classic` - classic edytor
75
120
  - `:inline` - inline editor
121
+ - `:decoupled` - decoupled editor
76
122
  - `:balloon` - balloon editor
77
123
  - `:multiroot` - editor with multiple editing areas
78
124
 
79
125
  The example below shows how to define a multiroot editor:
80
126
 
81
127
  ```rb
128
+ # config/initializers/ckeditor5.rb
129
+
82
130
  config.presets.define :custom do
83
131
  shape :multiroot
84
132
  end
@@ -89,6 +137,8 @@ end
89
137
  Defines the plugins to be included in the editor. You can specify multiple plugins by passing their names as arguments.
90
138
 
91
139
  ```rb
140
+ # config/initializers/ckeditor5.rb
141
+
92
142
  config.presets.define :custom do
93
143
  plugins :Bold, :Italic, :Underline, :Link
94
144
  end
@@ -104,23 +154,8 @@ Defines the toolbar items. You can use predefined items like `:undo`, `:redo`, `
104
154
  The `should_group_when_full` keyword argument determines whether the toolbar should group items when there is not enough space. It's set to `true` by default.
105
155
 
106
156
  ```rb
107
- config.presets.define :custom do
108
- # ... other configuration
109
-
110
- toolbar :undo, :redo, :|, :heading, :|, :bold, :italic, :underline, :|,
111
- :link, :insertImage, :ckbox, :mediaEmbed, :insertTable, :blockQuote, :|,
112
- :bulletedList, :numberedList, :todoList, :outdent, :indent
113
- end
114
- ```
115
-
116
- Keep in mind that the order of items is important, and you should install the corresponding plugins. You can find the list of available plugins in the [CKEditor 5 documentation](https://ckeditor.com/docs/ckeditor5/latest/framework/architecture/plugins.html).
117
-
118
- Defines the toolbar items. You can use predefined items like `:undo`, `:redo`, `:|` or specify custom items. There are few special items:
119
-
120
- - `:_` - breakpoint
121
- - `:|` - separator
157
+ # config/initializers/ckeditor5.rb
122
158
 
123
- ```rb
124
159
  config.presets.define :custom do
125
160
  # ... other configuration
126
161
 
@@ -137,6 +172,8 @@ Keep in mind that the order of items is important, and you should install the co
137
172
  Defines the visibility of the menubar. By default, it's set to `true`.
138
173
 
139
174
  ```rb
175
+ # config/initializers/ckeditor5.rb
176
+
140
177
  config.presets.define :custom do
141
178
  # ... other configuration
142
179
 
@@ -151,7 +188,11 @@ end
151
188
  Defines the language of the editor. You can pass the language code as an argument. Keep in mind that the UI and content language can be different. The example below shows how to set the Polish language for the UI and content:
152
189
 
153
190
  ```rb
191
+ # config/initializers/ckeditor5.rb
192
+
154
193
  config.presets.define :custom do
194
+ # ... other configuration
195
+
155
196
  language :pl
156
197
  end
157
198
  ```
@@ -159,7 +200,11 @@ end
159
200
  In order to set the language for the content, you can pass the `content` keyword argument:
160
201
 
161
202
  ```rb
203
+ # config/initializers/ckeditor5.rb
204
+
162
205
  config.presets.define :custom do
206
+ # ... other configuration
207
+
163
208
  language :en, content: :pl
164
209
  end
165
210
  ```
@@ -169,7 +214,11 @@ end
169
214
  Allows you to set custom configuration options. You can pass the name of the option and its value as arguments. The example below show how to set the default protocol for the link plugin to `https://`:
170
215
 
171
216
  ```rb
217
+ # config/initializers/ckeditor5.rb
218
+
172
219
  config.presets.define :custom do
220
+ # ... other configuration
221
+
173
222
  configure :link, {
174
223
  defaultProtocol: 'https://'
175
224
  }
@@ -183,7 +232,11 @@ Defines a plugin to be included in the editor. You can pass the name of the plug
183
232
  The example below show how to import Bold plugin from the `ckeditor5` npm package:
184
233
 
185
234
  ```rb
235
+ # config/initializers/ckeditor5.rb
236
+
186
237
  config.presets.define :custom do
238
+ # ... other configuration
239
+
187
240
  plugin :Bold
188
241
  end
189
242
  ```
@@ -191,15 +244,308 @@ end
191
244
  In order to import a plugin from a custom package, you can pass the `import_name` keyword argument:
192
245
 
193
246
  ```rb
247
+ # config/initializers/ckeditor5.rb
248
+
194
249
  config.presets.define :custom do
250
+ # ... other configuration
251
+
195
252
  plugin :YourPlugin, import_name: 'your-package'
196
253
  end
197
254
  ```
198
255
 
199
- ## License
256
+ </details>
257
+
258
+ ## Including CKEditor 5 assets 📦
259
+
260
+ To include CKEditor 5 assets in your application, you can use the `ckeditor5_assets` helper method. This method takes the version of CKEditor 5 as an argument and includes the necessary assets. It allows you to specify custom translations to be included.
261
+
262
+ Keep in mind that you need to include the assets in the `head` section of your layout. In examples below, we use `content_for` to include the assets in the `head` section.
263
+
264
+ ### Lazy loading 🚀
265
+
266
+ <details>
267
+ <summary>Loading JS and CSS Assets</summary>
268
+
269
+ All JS assets defined by the `ckeditor5_assets` helper method are loaded asynchronously. It means that the assets are loaded in the background without blocking the rendering of the page. However, the CSS assets are loaded synchronously to prevent the flash of unstyled content and ensure that the editor is styled correctly.
270
+
271
+ It has been achieved by using web components, together with import maps, which are supported by modern browsers. The web components are used to define the editor and its plugins, while the import maps are used to define the dependencies between the assets.
272
+
273
+ </details>
274
+
275
+ ### GPL usage 🆓
276
+
277
+ If you want to use CKEditor 5 under the GPL license, you can include the assets using the `ckeditor5_assets` helper method with the `version` keyword argument. The example below shows how to include the assets for version `43.3.0`:
278
+
279
+ ```erb
280
+ <!-- app/views/demos/index.html.erb -->
281
+
282
+ <% content_for :head do %>
283
+ <%= ckeditor5_assets version: '43.3.0' %>
284
+ <% end %>
285
+ ```
286
+
287
+ It'll include the necessary assets for the GPL license from one of the most popular CDNs. In our scenario, we use the `jsdelivr` CDN which is the default one.
288
+
289
+ In order to use `unpkg` CDN, you can pass the `cdn` keyword argument with the value `:unpkg`:
290
+
291
+ ```erb
292
+ <!-- app/views/demos/index.html.erb -->
293
+
294
+ <% content_for :head do %>
295
+ <%= ckeditor5_assets version: '43.3.0', cdn: :unpkg %>
296
+ <% end %>
297
+ ```
298
+
299
+ or using helper function:
300
+
301
+ ```erb
302
+ <!-- app/views/demos/index.html.erb -->
303
+
304
+ <% content_for :head do %>
305
+ <%= ckeditor5_jsdelivr_assets version: '43.3.0' %>
306
+ <% end %>
307
+ ```
308
+
309
+ Translating CKEditor 5 is possible by passing the `translations` keyword argument with the languages codes array. The example below shows how to include the Polish translations:
310
+
311
+ ```erb
312
+ <!-- app/views/demos/index.html.erb -->
313
+
314
+ <% content_for :head do %>
315
+ <%= ckeditor5_assets version: '43.3.0', translations: [:pl] %>
316
+ <% end %>
317
+ ```
318
+
319
+ Keep in mind, that you need to include the translations in the `config/initializers/ckeditor5.rb` file:
320
+
321
+ ```rb
322
+ # config/initializers/ckeditor5.rb
323
+
324
+ CKEditor5::Rails::Engine.configure do
325
+ presets.override :default do
326
+ language :pl
327
+ end
328
+ end
329
+ ```
330
+
331
+ ### Commercial usage 💰
332
+
333
+ <details>
334
+ <summary>Expand to show more</summary>
335
+
336
+ If you want to use CKEditor 5 under a commercial license, you can include the assets using the `ckeditor5_assets` helper method with the `license_key` keyword argument. The example below shows how to include the assets for the commercial license:
337
+
338
+ ```erb
339
+ <!-- app/views/demos/index.html.erb -->
340
+
341
+ <% content_for :head do %>
342
+ <%= ckeditor5_assets license_key: 'your-license-key' %>
343
+ <% end %>
344
+ ```
345
+
346
+ In this scenario, the assets are included from the official CKEditor 5 CDN which is more reliable and provides better performance, especially for commercial usage.
347
+
348
+ </details>
349
+
350
+ ## Editor placement 🏗️
351
+
352
+ The `ckeditor5_editor` helper renders CKEditor 5 instances in your views. Before using it, ensure you've included the necessary assets in your page's head section.
353
+
354
+ ### Classic editor 📝
355
+
356
+ The classic editor is the most common type of editor. It provides a toolbar with various formatting options like bold, italic, underline, and link.
357
+
358
+ It looks like this:
359
+
360
+ ![CKEditor 5 Classic Editor in Ruby on Rails application with Menubar](docs/classic-editor-with-toolbar.png)
361
+
362
+ The example below shows how to include the classic editor in your view:
363
+
364
+ ```erb
365
+ <!-- app/views/demos/index.html.erb -->
366
+
367
+ <% content_for :head do %>
368
+ <%= ckeditor5_assets version: '43.3.0' %>
369
+ <% end %>
370
+
371
+ <%= ckeditor5_editor style: 'width: 600px' %>
372
+ ```
373
+
374
+ You can pass the `style` keyword argument to the `ckeditor5_editor` helper to define the editor's style. The example above shows how to set the width of the editor to `600px`. However you can pass any HTML attribute you want, such as `class`, `id`, `data-*`, etc.
375
+
376
+ While example above uses predefined `:default` preset, you can use your custom presets by passing the `preset` keyword argument:
377
+
378
+ ```erb
379
+ <!-- app/views/demos/index.html.erb -->
380
+
381
+ <% content_for :head do %>
382
+ <%= ckeditor5_assets version: '43.3.0' %>
383
+ <% end %>
384
+
385
+ <%= ckeditor5_editor preset: :custom, style: 'width: 600px' %>
386
+ ```
387
+
388
+ If your configuration is even more complex, you can pass the `config` and `type` arguments with the configuration hash:
389
+
390
+ ```erb
391
+ <!-- app/views/demos/index.html.erb -->
392
+
393
+ <% content_for :head do %>
394
+ <%= ckeditor5_assets version: '43.3.0' %>
395
+ <% end %>
396
+
397
+ <%= ckeditor5_editor type: :classic, config: { plugins: [:Bold, :Italic], toolbar: [:Bold, :Italic] }, style: 'width: 600px' %>
398
+ ```
399
+
400
+ If you want to override the configuration of the editor specified in default or custom preset, you can pass the `extra_config` keyword argument with the configuration hash:
401
+
402
+ ```erb
403
+ <!-- app/views/demos/index.html.erb -->
404
+
405
+ <% content_for :head do %>
406
+ <%= ckeditor5_assets version: '43.3.0' %>
407
+ <% end %>
408
+
409
+ <%= ckeditor5_editor extra_config: { toolbar: [:Bold, :Italic] }, style: 'width: 600px' %>
410
+ ```
411
+
412
+ ### Multiroot editor 🌳
413
+
414
+ The multiroot editor allows you to create an editor with multiple editable areas. It's useful when you want to create a CMS with multiple editable areas on a single page.
415
+
416
+ - `ckeditor5_editor`: Defines the editor instance.
417
+ - `ckeditor5_editable`: Defines the editable areas within the editor.
418
+ - `ckeditor5_toolbar`: Defines the toolbar for the editor.
419
+
420
+ ![CKEditor 5 Multiroot Editor in Ruby on Rails application](docs/multiroot-editor.png)
421
+
422
+ If you want to use a multiroot editor, you can pass the `type` keyword argument with the value `:multiroot`:
423
+
424
+ ```erb
425
+ <!-- app/views/demos/index.html.erb -->
426
+
427
+ <% content_for :head do %>
428
+ <%= ckeditor5_assets version: '43.2.0' %>
429
+ <% end %>
430
+
431
+ <%= ckeditor5_editor type: :multiroot, style: 'width: 600px' do %>
432
+ <%= ckeditor5_toolbar %>
433
+ <br>
434
+ <%= ckeditor5_editable 'toolbar', style: 'border: 1px solid var(--ck-color-base-border);' do %>
435
+ This is a toolbar editable
436
+ <% end %>
437
+ <br>
438
+ <%= ckeditor5_editable 'content', style: 'border: 1px solid var(--ck-color-base-border)' %>
439
+ <br>
440
+ <% end %>
441
+ ```
442
+
443
+ Roots can be defined later to the editor by simply adding new elements rendered by `ckeditor5_editable` helper.
444
+
445
+ ### Inline editor 📝
446
+
447
+ Inline editor allows you to create an editor that can be placed inside any element. Keep in mind that inline editor does not work with `textarea` elements so it might be not suitable for all use cases.
448
+
449
+ ![CKEditor 5 Inline Editor in Ruby on Rails application](docs/inline-editor.png)
450
+
451
+ If you want to use an inline editor, you can pass the `type` keyword argument with the value `:inline`:
452
+
453
+ ```erb
454
+ <!-- app/views/demos/index.html.erb -->
455
+
456
+ <% content_for :head do %>
457
+ <%= ckeditor5_assets version: '43.2.0' %>
458
+ <% end %>
459
+
460
+ <%= ckeditor5_editor type: :inline, style: 'width: 600px' %>
461
+ ```
462
+
463
+ ### Balloon editor 🎈
464
+
465
+ Balloon editor is a floating toolbar editor that provides a minimalistic interface. It's useful when you want to create a simple editor with a floating toolbar.
466
+
467
+ ![CKEditor 5 Balloon Editor in Ruby on Rails application](docs/balloon-editor.png)
468
+
469
+ If you want to use a balloon editor, you can pass the `type` keyword argument with the value `:balloon`:
470
+
471
+ ```erb
472
+ <!-- app/views/demos/index.html.erb -->
473
+
474
+ <% content_for :head do %>
475
+ <%= ckeditor5_assets version: '43.2.0' %>
476
+ <% end %>
477
+
478
+ <%= ckeditor5_editor type: :balloon, style: 'width: 600px' %>
479
+ ```
480
+
481
+ ### Decoupled editor 🌐
482
+
483
+ Decoupled editor is a variant of classic editor that allows you to separate the editor from the content area. It's useful when you want to create a custom interface with the editor.
484
+
485
+ ![CKEditor 5 Decoupled Editor in Ruby on Rails application](docs/decoupled-editor.png)
486
+
487
+ If you want to use a decoupled editor, you can pass the `type` keyword argument with the value `:decoupled`:
488
+
489
+ ```erb
490
+ <% content_for :head do %>
491
+ <%= ckeditor5_assets version: '43.2.0', translations: [:pl, :es] %>
492
+ <% end %>
493
+
494
+ <style>
495
+ .menubar-container,
496
+ .editable-container,
497
+ .toolbar-container {
498
+ position: relative;
499
+ border: 1px solid red;
500
+ }
501
+
502
+ .menubar-container::after,
503
+ .editable-container::after,
504
+ .toolbar-container::after {
505
+ content: attr(class);
506
+ position: absolute;
507
+ background: red;
508
+ color: #fff;
509
+ top: 0;
510
+ right: 0;
511
+ font: 10px/2 monospace;
512
+ padding: .1em .3em;
513
+ }
514
+
515
+ .menubar-container,
516
+ .toolbar-container {
517
+ padding: 1em;
518
+ }
519
+
520
+ .editable-container {
521
+ padding: 3em;
522
+ overflow-y: scroll;
523
+ max-height: 300px;
524
+ }
525
+
526
+ .editable-container .ck-editor__editable {
527
+ min-height: 21cm;
528
+ padding: 2em;
529
+ border: 1px #D3D3D3 solid;
530
+ border-radius: var(--ck-border-radius);
531
+ background: white;
532
+ box-shadow: 0 0 5px rgba(0, 0, 0, 0.1);
533
+ }
534
+ </style>
535
+
536
+ <%= ckeditor5_editor type: :decoupled, style: 'width: 600px' do %>
537
+ <div class="menubar-container"><%= ckeditor5_menubar %></div>
538
+ <br>
539
+ <div class="toolbar-container"><%= ckeditor5_toolbar %></div>
540
+ <br>
541
+ <div class="editable-container"><%= ckeditor5_editable %></div>
542
+ <% end %>
543
+ ```
544
+
545
+ ## License 📜
200
546
 
201
547
  The MIT License (MIT)
202
- Copyright (c) Mateusz Bagiński / Łukasz Modliński
548
+ Mateusz Bagiński / Łukasz Modliński
203
549
 
204
550
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
205
551
 
@@ -94,7 +94,7 @@ class CKEditorComponent extends HTMLElement {
94
94
  return Promise.resolve(callback(this.instance));
95
95
  }
96
96
 
97
- return this.instancePromise.then(callback);
97
+ return this.instancePromise.promise.then(callback);
98
98
  }
99
99
 
100
100
  /**
@@ -115,25 +115,33 @@ class CKEditorComponent extends HTMLElement {
115
115
  /**
116
116
  * Initializes a new CKEditor instance
117
117
  * @private
118
- * @param {Record<string, HTMLElement>|CKEditorMultiRootEditablesTracker} editables - Editable elements
118
+ * @param {Record<string, HTMLElement>|CKEditorMultiRootEditablesTracker} editablesOrContent - Editable or content
119
119
  * @returns {Promise<import('ckeditor5').Editor>} Initialized editor instance
120
120
  * @throws {Error} When initialization fails
121
121
  */
122
- async #initializeEditor(editables) {
122
+ async #initializeEditor(editablesOrContent) {
123
123
  const Editor = await this.#getEditorConstructor();
124
124
  const [plugins, translations] = await Promise.all([
125
125
  this.#getPlugins(),
126
126
  this.#getTranslations()
127
127
  ]);
128
128
 
129
+ let content = editablesOrContent;
130
+
131
+ if (editablesOrContent instanceof CKEditorMultiRootEditablesTracker) {
132
+ content = editablesOrContent.getAll();
133
+ } else if (typeof editablesOrContent !== 'string') {
134
+ content = editablesOrContent.main;
135
+ }
136
+
129
137
  const instance = await Editor.create(
130
- editables instanceof CKEditorMultiRootEditablesTracker
131
- ? editables.getAll()
132
- : editables.main,
138
+ content,
133
139
  {
134
140
  ...this.#getConfig(),
141
+ ...translations.length && {
142
+ translations
143
+ },
135
144
  plugins,
136
- translations
137
145
  }
138
146
  );
139
147
 
@@ -158,21 +166,21 @@ class CKEditorComponent extends HTMLElement {
158
166
 
159
167
  this.style.display = 'block';
160
168
 
161
- if (!this.#isMultiroot()) {
169
+ if (!this.isMultiroot() && !this.isDecoupled()) {
162
170
  this.innerHTML = `<${this.#editorElementTag}></${this.#editorElementTag}>`;
163
171
  }
164
172
 
165
173
  // Let's track changes in editables if it's a multiroot editor.
166
- const editables = this.#queryEditables();
167
-
168
- if(this.#isMultiroot()) {
169
- this.editables = new CKEditorMultiRootEditablesTracker(this, editables);
174
+ if(this.isMultiroot()) {
175
+ this.editables = new CKEditorMultiRootEditablesTracker(this, this.#queryEditables());
176
+ } else if (this.isDecoupled()) {
177
+ this.editables = null;
170
178
  } else {
171
- this.editables = editables;
179
+ this.editables = this.#queryEditables();
172
180
  }
173
181
 
174
182
  try {
175
- this.instance = await this.#initializeEditor(this.editables);
183
+ this.instance = await this.#initializeEditor(this.editables || this.#getConfig().initialData || '');
176
184
  this.instancePromise.resolve(this.instance);
177
185
  } catch (err) {
178
186
  this.instancePromise.reject(err);
@@ -186,10 +194,20 @@ class CKEditorComponent extends HTMLElement {
186
194
  * @private
187
195
  * @returns {boolean}
188
196
  */
189
- #isMultiroot() {
197
+ isMultiroot() {
190
198
  return this.getAttribute('type') === 'MultiRootEditor';
191
199
  }
192
200
 
201
+ /**
202
+ * Checks if current editor is decoupled type
203
+ *
204
+ * @private
205
+ * @returns {boolean}
206
+ */
207
+ isDecoupled() {
208
+ return this.getAttribute('type') === 'DecoupledEditor';
209
+ }
210
+
193
211
  /**
194
212
  * Parses editor configuration from config attribute
195
213
  *
@@ -208,7 +226,11 @@ class CKEditorComponent extends HTMLElement {
208
226
  * @throws {Error} When required editables are missing
209
227
  */
210
228
  #queryEditables() {
211
- if (this.#isMultiroot()) {
229
+ if (this.isDecoupled()) {
230
+ return {};
231
+ }
232
+
233
+ if (this.isMultiroot()) {
212
234
  const editables = [...this.querySelectorAll('ckeditor-editable-component')];
213
235
 
214
236
  return editables.reduce((acc, element) => {
@@ -444,7 +466,8 @@ class CKEditorEditableComponent extends HTMLElement {
444
466
  * @returns {string} The name attribute value
445
467
  */
446
468
  get name() {
447
- return this.getAttribute('name');
469
+ // The default value is set mainly for decoupled editors where the name is not required.
470
+ return this.getAttribute('name') || 'editable';
448
471
  }
449
472
 
450
473
  /**
@@ -462,16 +485,28 @@ class CKEditorEditableComponent extends HTMLElement {
462
485
  * @throws {Error} If not used as child of ckeditor-component
463
486
  */
464
487
  connectedCallback() {
465
- const editorComponent = this.#queryEditorElement();
488
+ execIfDOMReady(() => {
489
+ const editorComponent = this.#queryEditorElement();
466
490
 
467
- if (!editorComponent ) {
468
- throw new Error('ckeditor-editable-component must be a child of ckeditor-component');
469
- }
491
+ if (!editorComponent ) {
492
+ throw new Error('ckeditor-editable-component must be a child of ckeditor-component');
493
+ }
470
494
 
471
- this.innerHTML = `<div>${this.innerHTML}</div>`;
472
- this.style.display = 'block';
495
+ this.innerHTML = `<div>${this.innerHTML}</div>`;
496
+ this.style.display = 'block';
497
+
498
+ if (editorComponent.isDecoupled()) {
499
+ editorComponent.runAfterEditorReady(editor => {
500
+ this.appendChild(editor.ui.view[this.name].element);
501
+ });
502
+ } else {
503
+ if (!this.name) {
504
+ throw new Error('Editable component missing required "name" attribute');
505
+ }
473
506
 
474
- editorComponent.editables[this.name] = this;
507
+ editorComponent.editables[this.name] = this;
508
+ }
509
+ });
475
510
  }
476
511
 
477
512
  /**
@@ -522,28 +557,31 @@ class CKEditorEditableComponent extends HTMLElement {
522
557
  * @returns {CKEditorComponent|null} Parent editor component or null if not found
523
558
  */
524
559
  #queryEditorElement() {
525
- return this.closest('ckeditor-component');
560
+ return this.closest('ckeditor-component') || document.body.querySelector('ckeditor-component');
526
561
  }
527
562
  }
528
563
 
529
564
  /**
530
- * Custom HTML element that represents a CKEditor toolbar component.
531
- * Manages the toolbar placement and integration with the main editor component.
565
+ * Custom HTML element that represents a CKEditor UI part component.
566
+ * It helpers with management of toolbar and other elements.
532
567
  *
533
568
  * @extends HTMLElement
534
- * @customElement ckeditor-toolbar
569
+ * @customElement ckeditor-ui-part-component
535
570
  * @example
536
- * <ckeditor-toolbar></ckeditor-toolbar>
571
+ * <ckeditor-ui-part-component></ckeditor-ui-part-component>
537
572
  */
538
- class CKEditorToolbarComponent extends HTMLElement {
573
+ class CKEditorUIPartComponent extends HTMLElement {
539
574
  /**
540
575
  * Lifecycle callback when element is added to DOM
541
576
  * Adds the toolbar to the editor UI
542
577
  */
543
- async connectedCallback() {
544
- const editor = await this.#queryEditorElement().instancePromise.promise;
578
+ connectedCallback() {
579
+ execIfDOMReady(async () => {
580
+ const uiPart = this.getAttribute('name');
581
+ const editor = await this.#queryEditorElement().instancePromise.promise;
545
582
 
546
- this.appendChild(editor.ui.view.toolbar.element);
583
+ this.appendChild(editor.ui.view[uiPart].element);
584
+ });
547
585
  }
548
586
 
549
587
  /**
@@ -553,7 +591,7 @@ class CKEditorToolbarComponent extends HTMLElement {
553
591
  * @returns {CKEditorComponent|null} Parent editor component or null if not found
554
592
  */
555
593
  #queryEditorElement() {
556
- return this.closest('ckeditor-component');
594
+ return this.closest('ckeditor-component') || document.body.querySelector('ckeditor-component');
557
595
  }
558
596
  }
559
597
 
@@ -600,4 +638,4 @@ function loadAsyncImports(imports = []) {
600
638
 
601
639
  customElements.define('ckeditor-component', CKEditorComponent);
602
640
  customElements.define('ckeditor-editable-component', CKEditorEditableComponent);
603
- customElements.define('ckeditor-toolbar-component', CKEditorToolbarComponent);
641
+ customElements.define('ckeditor-ui-part-component', CKEditorUIPartComponent);
@@ -25,15 +25,24 @@ module CKEditor5::Rails
25
25
  context: context
26
26
  )
27
27
 
28
- render_editor_component(editor_props, html_attributes, &(type == :multiroot ? block : nil))
28
+ render_editor_component(editor_props, html_attributes,
29
+ &(%i[multiroot decoupled].include?(type) ? block : nil))
29
30
  end
30
31
 
31
- def ckeditor5_editable(name, **kwargs, &block)
32
+ def ckeditor5_editable(name = nil, **kwargs, &block)
32
33
  tag.send(:'ckeditor-editable-component', name: name, **kwargs, &block)
33
34
  end
34
35
 
36
+ def ckeditor5_ui_part(name, **kwargs, &block)
37
+ tag.send(:'ckeditor-ui-part-component', name: name, **kwargs, &block)
38
+ end
39
+
35
40
  def ckeditor5_toolbar(**kwargs)
36
- tag.send(:'ckeditor-toolbar-component', **kwargs)
41
+ ckeditor5_ui_part('toolbar', **kwargs)
42
+ end
43
+
44
+ def ckeditor5_menubar(**kwargs)
45
+ ckeditor5_ui_part('menuBarView', **kwargs)
37
46
  end
38
47
 
39
48
  private
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module CKEditor5::Rails
4
- VERSION = '1.0.4'
4
+ VERSION = '1.0.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ckeditor5
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.4
4
+ version: 1.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mateusz Bagiński