/** * @license Copyright (c) 2014-2024, CKSource Holding sp. z o.o. All rights reserved. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license */ import { ClassicEditor } from '@ckeditor/ckeditor5-editor-classic'; import { Style } from '@ckeditor/ckeditor5-style'; import { Alignment } from '@ckeditor/ckeditor5-alignment'; import { Autoformat } from '@ckeditor/ckeditor5-autoformat'; import { Autosave } from '@ckeditor/ckeditor5-autosave'; import { Bold, Italic, Strikethrough, Underline, Subscript, Superscript } from '@ckeditor/ckeditor5-basic-styles'; import { BlockQuote } from '@ckeditor/ckeditor5-block-quote'; import { CloudServices } from '@ckeditor/ckeditor5-cloud-services'; import type { EditorConfig, PluginConstructor } from '@ckeditor/ckeditor5-core'; import { Essentials } from '@ckeditor/ckeditor5-essentials'; import { FindAndReplace } from '@ckeditor/ckeditor5-find-and-replace'; import { FontBackgroundColor, FontColor, FontFamily, FontSize, } from '@ckeditor/ckeditor5-font'; import { Heading, Title } from '@ckeditor/ckeditor5-heading'; import { Highlight } from '@ckeditor/ckeditor5-highlight'; import { HorizontalLine } from '@ckeditor/ckeditor5-horizontal-line'; import { DataFilter, GeneralHtmlSupport } from '@ckeditor/ckeditor5-html-support'; import { AutoImage, Image, ImageCaption, ImageResize, ImageStyle, ImageToolbar, ImageResizeEditing, ImageResizeHandles, PictureEditing, ImageInsertViaUrl } from '@ckeditor/ckeditor5-image'; import { Indent, IndentBlock } from '@ckeditor/ckeditor5-indent'; import { AutoLink, Link } from '@ckeditor/ckeditor5-link'; import { List, ListProperties } from '@ckeditor/ckeditor5-list'; import { MediaEmbed } from '@ckeditor/ckeditor5-media-embed'; import { PageBreak } from '@ckeditor/ckeditor5-page-break'; import { Paragraph } from '@ckeditor/ckeditor5-paragraph'; import { PasteFromOffice } from '@ckeditor/ckeditor5-paste-from-office'; import { RemoveFormat } from '@ckeditor/ckeditor5-remove-format'; import { SpecialCharacters, SpecialCharactersEssentials, SpecialCharactersLatin } from '@ckeditor/ckeditor5-special-characters'; import { Table, TableCaption, TableCellProperties, TableColumnResize, TableProperties, TableToolbar } from '@ckeditor/ckeditor5-table'; import { TextTransformation } from '@ckeditor/ckeditor5-typing'; import { Undo } from '@ckeditor/ckeditor5-undo'; import { WordCount } from '@ckeditor/ckeditor5-word-count'; import { SourceEditing } from '@ckeditor/ckeditor5-source-editing'; // You can read more about extending the build with additional plugins in the "Installing plugins" guide. // See https://ckeditor.com/docs/ckeditor5/latest/installation/plugins/installing-plugins.html for details. import { DataProcessor, ViewDocument, ViewDocumentFragment, MatcherPattern } from '@ckeditor/ckeditor5-engine'; import { Plugin } from '@ckeditor/ckeditor5-core'; class PlainTextProcessor implements DataProcessor { editor : any constructor(editor : any) { this.editor = editor } /** * Restituisce il contenuto della textarea dopo averlo processato, * in questa implementazione sostituisce i tag
con tag
rimuovendo
* il primo tag
* @param viewFragment
* @returns
*/
public toData( viewFragment: ViewDocumentFragment ): string {
var htmlData = this.editor.data.htmlProcessor.toData( viewFragment ).replace(/<\/?p>/g, '
');
const tmp = document.createElement( 'DIV' );
tmp.innerHTML = htmlData;
if (htmlData.startsWith('
')) {
htmlData = htmlData.substring(4);
}
return htmlData;
}
/**
* Effettua il primo rendering del contenuto
* @param data
* @returns
*/
public toView( data: string ): ViewDocumentFragment {
var escapedData = data
.replace(/<\/div>/g, '
')
.replace(/<\/?p>/g, '
')
.replace('
','||br||')
.replace( /&/g, '&' )
.replace( //g, '>' )
.trim()
.replace('||br||','
')
var escapedData = data
// Wrap the escaped data in a paragraph element.
const viewFragment = this.editor.data.htmlProcessor.toView( escapedData );
return viewFragment;
}
public registerRawContentMatcher( pattern: MatcherPattern ): void {}
public useFillerType( type: 'default' | 'marked' ): void {}
}
class PlainText extends Plugin {
static get pluginName() {
return "PlainText"
}
init() : void {
this.editor.data.processor = new PlainTextProcessor(this.editor)
}
}
class Editor extends ClassicEditor {
public static override builtinPlugins : PluginConstructor[] = [
PlainText,
SourceEditing,
Subscript,
Superscript,
Alignment,
Autoformat,
AutoImage,
AutoLink,
Autosave,
BlockQuote,
Bold,
CloudServices,
DataFilter,
Essentials,
FindAndReplace,
FontBackgroundColor,
FontColor,
FontFamily,
FontSize,
GeneralHtmlSupport,
Heading,
Highlight,
HorizontalLine,
Image,
ImageCaption,
ImageResize,
ImageStyle,
ImageToolbar,
ImageInsertViaUrl,
Image,
ImageResizeEditing,
ImageResizeHandles,
Indent,
IndentBlock,
Italic,
Link,
List,
ListProperties,
MediaEmbed,
PageBreak,
Paragraph,
PasteFromOffice,
PictureEditing,
RemoveFormat,
SpecialCharacters,
SpecialCharactersEssentials,
SpecialCharactersLatin,
Strikethrough,
Style,
Table,
TableCaption,
TableCellProperties,
TableColumnResize,
TableProperties,
TableToolbar,
TextTransformation,
Title,
Underline,
Undo,
WordCount
];
public static override defaultConfig: EditorConfig = {
toolbar: {
items: [
'undo',
'redo',
'|',
'alignment',
'|',
'bold',
'italic',
'underline',
'|',
'fontColor',
'highlight',
'|',
'bulletedList',
'numberedList',
'|',
'outdent',
'indent'
]
},
language: 'it',
table: {
contentToolbar: [
'tableColumn',
'tableRow',
'mergeTableCells',
'tableCellProperties',
'tableProperties'
]
}
};
}
export default Editor