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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
| <!DOCTYPE html>
<html lang="fr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CKEditor 5 DecoupledEditor</title>
<meta name="Author" content="NoSmoking">
<meta name="DVP-discussion" content="dxxxxxxx">
<meta name="description" content="CKEditor v5 : chargement d'image dans l'éditeur.">
<script src="https://cdn.ckeditor.com/ckeditor5/34.1.0/decoupled-document/ckeditor.js"></script>
<script src="https://cdn.ckeditor.com/ckeditor5/34.1.0/decoupled-document/translations/fr.js"></script>
<style>
html, body {margin: 0;padding: 0;font: 1em/1.5 Verdana,sans-serif;}
header h1 {margin: .25em 0;color: #069;}
time {float: right;margin: .5em;font-size: 0.9em;color: #888;}
main {display: block;margin: auto;max-width: 60em;}
</style>
</head>
<body>
<main>
<header>
<time datetime="2022-06-15">Juin 2022</time>
<h1>CKEditor 5 DecoupledEditor</h1>
</header>
<div class="editor-container">
<!-- The toolbar will be rendered in this container. -->
<div id="toolbar-editor"></div>
<!-- This container will become the editable. -->
<div id="editor">
<h1>Titre</h1>
<p>Paragraphe</p>
</div>
</div>
</main>
<script>
/**
* https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-upload/src/adapters/base64uploadadapter.js
*/
class Adapter {
/**
* Creates a new adapter instance.
* @param {module:upload/filerepository~FileLoader} loader
*/
constructor(loader) {
/**
* `FileLoader` instance to use during the upload.
* @member {module:upload/filerepository~FileLoader} #loader
*/
this.loader = loader;
}
/**
* Starts the upload process.
* @see module:upload/filerepository~UploadAdapter#upload
* @returns {Promise}
*/
upload() {
return new Promise((resolve, reject) => {
const reader = this.reader = new window.FileReader();
reader.addEventListener('load', () => {
resolve({
default: reader.result
});
});
reader.addEventListener('error', err => {
reject(err);
});
reader.addEventListener('abort', () => {
reject();
});
this.loader.file.then(file => {
reader.readAsDataURL(file);
});
});
}
/**
* Aborts the upload process.
* @see module:upload/filerepository~UploadAdapter#abort
* @returns {Promise}
*/
abort() {
this.reader.abort();
}
}
function CustomUploadAdapterPlugin(editor) {
editor.plugins.get('FileRepository')
.createUploadAdapter = (loader) => {
return new Adapter(loader);
};
}
/**
*
*/
DecoupledEditor
.create(document.querySelector('#editor'), {
extraPlugins: [CustomUploadAdapterPlugin],
language: "fr",
})
.then(editor => {
window.editor = editor;
const toolbarContainer = document.querySelector('#toolbar-editor');
toolbarContainer.appendChild(editor.ui.view.toolbar.element);
editor.model.document.on( 'change:data', () => {
//console.log(editor.getData() );
} );
})
.catch(error => {
console.error(error);
});
</script>
</body>
</html> |
Partager