Skip to content

Commit 3461b57

Browse files
authored
Merge pull request #140 from mostafaznv/dev
feat: force paste as plain text #139
2 parents 8a3cc38 + 6b7efe4 commit 3461b57

File tree

8 files changed

+130
-9
lines changed

8 files changed

+130
-9
lines changed

config/nova-ckeditor.php

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@
112112

113113
'content-lang' => 'en',
114114

115+
'force-paste-as-plain-text' => false,
116+
115117
'ui-language' => [
116118
'name' => 'en',
117119

dist/js/field.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/SUMMARY.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* [Height](advanced-usage/ckeditor-field-options/height.md)
2828
* [Limit On Index](advanced-usage/ckeditor-field-options/limit-on-index.md)
2929
* [Content Language](advanced-usage/ckeditor-field-options/content-language.md)
30+
* [Force Paste as Plain Text](advanced-usage/ckeditor-field-options/force-page-as-plain-text.md)
3031
* [Text Part Language](advanced-usage/ckeditor-field-options/text-part-language.md)
3132
* [General HTML Support](advanced-usage/ckeditor-field-options/general-html-support.md)
3233
* [Group Items In Overflow Mode](advanced-usage/ckeditor-field-options/group-items-in-overflow-mode.md)
@@ -52,6 +53,7 @@
5253
* [Items](advanced-usage/configuration/toolbars/toolbar-1/items.md)
5354
* [Options](advanced-usage/configuration/toolbars/toolbar-1/options.md)
5455
* [Content Lang](advanced-usage/configuration/toolbars/toolbar-1/content-lang.md)
56+
* [Force Past as Plain Text](advanced-usage/configuration/toolbars/toolbar-1/force-page-as-plain-text.md)
5557
* [UI Language](advanced-usage/configuration/toolbars/toolbar-1/ui-language/README.md)
5658
* [UI Language Name](advanced-usage/configuration/toolbars/toolbar-1/ui-language/ui-language-name.md)
5759
* [UI Language Script](advanced-usage/configuration/toolbars/toolbar-1/ui-language/ui-language-script.md)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
description: forcePasteAsPlainText
3+
---
4+
5+
# Force Paste as Plain Text
6+
7+
<table><thead><tr><th>Argument</th><th width="141">Type</th><th width="149" data-type="checkbox">Required</th><th>Default</th></tr></thead><tbody><tr><td>status</td><td>bool</td><td>false</td><td>true</td></tr></tbody></table>
8+
9+
In some cases, you might want to paste content from your clipboard as plain text, removing any HTML tags.
10+
11+
By default, CKEditor preserves the structure of the original content and ensures HTML is safely maintained. However, if you’d prefer to paste content without any formatting, as plain text, you can use the `forcePasteAsPlainText` function.
12+
13+
14+
15+
```php
16+
use Mostafaznv\NovaCkEditor\CkEditor;
17+
18+
class Article extends Resource
19+
{
20+
public function fields(Request $request): array
21+
{
22+
return [
23+
CkEditor::make(trans('Content'), 'content')
24+
->forcePasteAsPlainText()
25+
];
26+
}
27+
}
28+
```
29+
30+
{% hint style="info" %}
31+
This feature has been available since <mark style="color:red;">v7.5.0</mark>
32+
{% endhint %}
33+
34+
35+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
description: toolbars.toolbar-1.force-paste-as-plain-text
3+
---
4+
5+
# Force Past as Plain Text
6+
7+
<table><thead><tr><th width="390">Property Name</th><th width="152.33333333333331">Type</th><th>Default</th></tr></thead><tbody><tr><td>toolbars.toolbar-1.force-paste-as-plain-text</td><td>bool</td><td>false</td></tr></tbody></table>
8+
9+
For certain cases, you may want to paste content from your clipboard as plain text, stripping away any HTML tags.
10+
11+
By default, when pasting content into the editor, CKEditor will try to maintain the original content’s structure and keep the HTML in a safe way. However, if you’d prefer to paste content without any formatting and only as plain text, you can set <mark style="color:red;">`toolbars.toolbar-1.force-paste-as-plain-text`</mark> to <mark style="color:red;">`true`</mark> in the configuration.
12+
13+
14+
15+
{% hint style="info" %}
16+
This feature has been available since <mark style="color:red;">v7.5.0</mark>
17+
{% endhint %}
18+
19+
20+

docs/getting-started/installation.md

+29-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ php artisan vendor:publish --provider="Mostafaznv\NovaCkEditor\FieldServiceProvi
3535

3636
#### 3. Prepare the migration, configurations and models
3737

38-
After publishing stubs, essential `Image`, `Video` and `Audio` classes will be created in the `app/Models` and `app/Nova/Resources` directories respectively. These classes are used for the `media-picker` in the CKEditor field.
38+
After publishing stubs, essential `Image`, `Video`, `Audio` and `File` classes will be created in the `app/Models` and `app/Nova/Resources` directories respectively. These classes are used for the `media-picker` in the CKEditor field.
3939

4040
{% tabs %}
4141
{% tab title="Image" %}
@@ -109,8 +109,30 @@ If you wish to modify the disk name, remember to update it in the `App\Nova\Reso
109109
{% endhint %}
110110
{% endtab %}
111111
112+
{% tab title="File" %}
113+
You should create a disk in `config/filesystems.php`:
114+
115+
```php
116+
'disks' => [
117+
'file' => [
118+
'driver' => 'local',
119+
'root' => public_path('uploads/file'),
120+
'url' => env('APP_URL') . '/uploads/file',
121+
]
122+
]
123+
```
124+
125+
{% hint style="info" %}
126+
If you wish to modify the disk name, remember to update it in the `App\Nova\Resources\File` class as well. The third argument of the make function in the FileUpload field corresponds to the disk name.
127+
{% endhint %}
128+
129+
{% hint style="info" %}
130+
This feature was introduced in version <mark style="color:red;">7.3.0</mark> of the NovaCKEditor
131+
{% endhint %}
132+
{% endtab %}
133+
112134
{% tab title="Migration" %}
113-
**If you have chosen Larupload**, there is no need to make any changes to the migration file. You can refer to the [nova-video](https://github.com/mostafaznv/nova-video) and [larupload](https://github.com/mostafaznv/larupload) documentations for additional configuration options.
135+
**If you have chosen Larupload**, there is no need to make any changes to the videos migration file. You can refer to the [nova-video](https://github.com/mostafaznv/nova-video) and [larupload](https://github.com/mostafaznv/larupload) documentations for additional configuration options.
114136
115137
**If you have chosen Laravel's file system**, you must make some changes to the migration file. In the migration file, replace the <mark style="color:red;">upload column</mark> with a <mark style="color:red;">string column</mark>.
116138
@@ -134,7 +156,7 @@ class CreateVideosTable extends Migration
134156
{% endtab %}
135157
136158
{% tab title="Model" %}
137-
**If you have chosen Larupload**, there is no need to make any changes to the model. You can refer to the [nova-video](https://github.com/mostafaznv/nova-video) and [larupload](https://github.com/mostafaznv/larupload) documentations for additional configuration options.
159+
**If you have chosen Larupload**, there is no need to make any changes to the `Video` model. You can refer to the [nova-video](https://github.com/mostafaznv/nova-video) and [larupload](https://github.com/mostafaznv/larupload) documentations for additional configuration options.
138160
139161
**If you have chosen Laravel's file system**, you must make some changes to the model. Remove the Larupload <mark style="color:red;">trait</mark> and the <mark style="color:red;">attachments function</mark> from the model.
140162
@@ -170,6 +192,10 @@ class Video extends Model
170192
{% endtab %}
171193
{% endtabs %}
172194
195+
196+
197+
198+
173199
#### 4. Migrate
174200
175201
```shell

resources/js/fields/editor-field.vue

+19-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<template>
22
<default-field :field="currentField" :errors="errors" :full-width-content="true">
33
<template #field>
4-
<textarea ref="editor" class="hidden" :id="currentField.attribute" :class="errorClasses" :value="value" />
4+
<textarea ref="editor" class="hidden" :id="currentField.attribute" :class="errorClasses" :value="value"/>
55

6-
<p v-if="currentField.helpText" v-html="currentField.helpText" class="help-text help-text mt-2" />
6+
<p v-if="currentField.helpText" v-html="currentField.helpText" class="help-text help-text mt-2"/>
77

88
<media-browser
99
@select="$options[editorName].execute('mediaBrowser', $event)"
@@ -94,7 +94,7 @@ export default {
9494
9595
headers: {
9696
...CkEditor.defaultConfig.simpleUpload.headers,
97-
'X-Toolbar': this.currentField.toolbarName
97+
'X-Toolbar': this.currentField.toolbarName
9898
},
9999
},
100100
...toolbarOptions
@@ -128,6 +128,20 @@ export default {
128128
if (this.currentField.readonly) {
129129
editor.enableReadOnlyMode(this.$options[this.editorUUID]);
130130
}
131+
132+
133+
if (this.currentField.forcePasteAsPlainText) {
134+
editor.editing.view.document.on('clipboardInput', (evt, data) => {
135+
evt.stop()
136+
137+
editor.model.change(writer => {
138+
writer.insertText(
139+
data.dataTransfer.getData('text/plain'),
140+
editor.model.document.selection.getFirstPosition()
141+
)
142+
})
143+
})
144+
}
131145
})
132146
.catch((e) => {
133147
console.log(e)
@@ -170,7 +184,7 @@ export default {
170184
if (Array.isArray(url)) {
171185
const urls = url
172186
173-
for (let j = 0; j< urls.length; j++) {
187+
for (let j = 0; j < urls.length; j++) {
174188
urls[j] = RegexParser(urls[j])
175189
}
176190
@@ -320,7 +334,7 @@ export default {
320334
321335
<style lang="sass">
322336
.ck.ck-reset_all, .ck.ck-reset_all *
323-
// direction: ltr !important
337+
// direction: ltr !important
324338
325339
.ck-content.ck-editor__editable
326340
resize: vertical

src/CkEditor.php

+22
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,13 @@ class CkEditor extends Field
6464
*/
6565
public string $contentLanguage;
6666

67+
/**
68+
* Force Paste As Plain Text
69+
*
70+
* @var bool
71+
*/
72+
public bool $forcePasteAsPlainText;
73+
6774
/**
6875
* Text Part Language
6976
*
@@ -208,6 +215,19 @@ public function contentLanguage(string $lang): self
208215
return $this;
209216
}
210217

218+
/**
219+
* Set Force Paste As Plain Text
220+
*
221+
* @param bool $status
222+
* @return $this
223+
*/
224+
public function forcePasteAsPlainText(bool $status = true): self
225+
{
226+
$this->forcePasteAsPlainText = $status;
227+
228+
return $this;
229+
}
230+
211231
/**
212232
* Set Text Part Language
213233
*
@@ -331,6 +351,7 @@ public function jsonSerialize(): array
331351
'height' => $this->height,
332352
'indexLimit' => $this->indexLimit,
333353
'contentLanguage' => $this->contentLanguage,
354+
'forcePasteAsPlainText' => $this->forcePasteAsPlainText,
334355
'textPartLanguage' => $this->textPartLanguage,
335356
'htmlSupport' => $this->htmlSupport,
336357
'uiLanguage' => $this->uiLanguage,
@@ -421,6 +442,7 @@ private function prepareToolbar(string $toolbar, array $items = null): void
421442
$this->fileBrowser = $toolbar['browser']['file'] ?? false;
422443
$this->snippetBrowser = $this->prepareSnippets($toolbar['snippets']);
423444
$this->contentLanguage = $toolbar['content-lang'];
445+
$this->forcePasteAsPlainText = $toolbar['force-paste-as-plain-text'] ?? false;
424446
$this->textPartLanguage = $toolbar['text-part-language'] ?? $defaultTextPartLanguage;
425447
$this->htmlSupport = $toolbar['html-support'] ?? $defaultHtmlSupport;
426448
$this->uiLanguage = $toolbar['ui-language']['name'] ?? 'en';

0 commit comments

Comments
 (0)