Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ckeditor img plugin #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions static/js/lib/ckeditor/CKEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import CodeBlockPlugin from "@ckeditor/ckeditor5-code-block/src/codeblock"
import CodePlugin from "@ckeditor/ckeditor5-basic-styles/src/code"
import Mathematics from "ckeditor5-math/src/math"
import MathSyntax from "./plugins/MathSyntax"
import Image from "./plugins/Image"

import { Editor } from "@ckeditor/ckeditor5-core"

Expand Down Expand Up @@ -66,6 +67,7 @@ const SUPPORTED_PROGRAMMING_LANGUAGES = [

export const FullEditorConfig = {
plugins: [
Image,
EssentialsPlugin,
AutoformatPlugin,
BoldPlugin,
Expand Down Expand Up @@ -100,6 +102,7 @@ export const FullEditorConfig = {
],
toolbar: {
items: [
"img",
"heading",
"|",
"bold",
Expand Down Expand Up @@ -141,6 +144,7 @@ export const FullEditorConfig = {

export const MinimalEditorConfig = {
plugins: [
Image,
EssentialsPlugin,
AutoformatPlugin,
BoldPlugin,
Expand All @@ -160,6 +164,7 @@ export const MinimalEditorConfig = {
],
toolbar: {
items: [
"img",
"bold",
"italic",
"code",
Expand Down
9 changes: 9 additions & 0 deletions static/js/lib/ckeditor/plugins/Image.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ImageEditing from './ImageEditing';
import ImageUI from './ImageUI';
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class Image extends Plugin {
static get requires() {
return [ ImageEditing, ImageUI ];
}
}
44 changes: 44 additions & 0 deletions static/js/lib/ckeditor/plugins/ImageEditing.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class ImageEditing extends Plugin {
init() {
this._defineSchema();
this._defineConverters();
}

_defineSchema() {
const schema = this.editor.model.schema;

schema.extend( '$text', {
allowAttributes: [ 'img' ]
} );
}

_defineConverters() {
const conversion = this.editor.conversion;

conversion.for( 'downcast' ).attributeToElement( {
model: 'img',
view: ( modelAttributeValue:any, conversionApi:any ) => {
const { writer } = conversionApi;
return writer.createAttributeElement( 'img', {
src: modelAttributeValue
} );
}
} );
// Conversion from a view element to a model attribute.
conversion.for( 'upcast' ).elementToAttribute( {
view: {
name: 'img',
attributes: [ 'src' ]
},
model: {
key: 'img',
value: viewElement => {
const img = viewElement.getAttribute( 'src' );
return img;
}
}
} );
}
}
29 changes: 29 additions & 0 deletions static/js/lib/ckeditor/plugins/ImageUI.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import ButtonView from "@ckeditor/ckeditor5-ui/src/button/buttonview"

import Plugin from '@ckeditor/ckeditor5-core/src/plugin';

export default class ImageUI extends Plugin {
init(): void {
const editor = this.editor;

editor.ui.componentFactory.add( 'img', () => {
const button = new ButtonView();
button.set({
label: "img",
withText: true,
tooltip: true
})
this.listenTo( button, 'execute', () => {
const src = 'https://pbs.twimg.com/media/E4OzGLfX0AoMeuD.jpg';
editor.model.change( (writer:any) => {
editor.model.insertContent(
// Create a text node with the img attribute.
writer.createText('img',{ img: src } )
);
} );
} );

return button;
} );
}
}