1+ import { Node , updateAttrsDialog } from "django-prose-editor/editor" ;
2+
3+ const imageDialogImpl = ( editor , attrs , options ) => {
4+ const properties = {
5+ src : {
6+ type : "string" ,
7+ title : gettext ( "Source" ) ,
8+ required : true ,
9+ } ,
10+ alt : {
11+ type : "string" ,
12+ title : gettext ( "Alt Text" ) ,
13+ required : true ,
14+ } ,
15+ title : {
16+ type : "string" ,
17+ title : gettext ( "Title" ) ,
18+ } ,
19+ width : {
20+ type : "number" ,
21+ title : gettext ( "Width" ) ,
22+ } ,
23+ height : {
24+ type : "number" ,
25+ title : gettext ( "Height" ) ,
26+ } ,
27+ } ;
28+
29+ return updateAttrsDialog ( properties , {
30+ title : gettext ( "Add or edit image" ) ,
31+ } ) ( editor , attrs ) ;
32+ } ;
33+
34+ const ImageDialog = async ( editor , attrs , options ) => {
35+ attrs = attrs || { } ;
36+ attrs = await imageDialogImpl ( editor , attrs , options ) ;
37+ if ( attrs ) {
38+ return attrs ;
39+ }
40+ } ;
41+
42+ export const AddImage = Node . create ( {
43+ name : "image" ,
44+ content : "inline*" ,
45+ group : "block" ,
46+ isolating : true ,
47+
48+ addAttributes ( ) {
49+ return {
50+ src : {
51+ default : null ,
52+ } ,
53+ alt : {
54+ default : null ,
55+ } ,
56+ title : {
57+ default : null ,
58+ } ,
59+ width : {
60+ default : null ,
61+ } ,
62+ height : {
63+ default : null ,
64+ } ,
65+ } ;
66+ } ,
67+ parseHTML ( ) {
68+ return [
69+ {
70+ tag : "img[src]" ,
71+ getAttrs : ( dom ) => ( {
72+ src : dom . getAttribute ( "src" ) ,
73+ alt : dom . getAttribute ( "alt" ) ,
74+ title : dom . getAttribute ( "title" ) ,
75+ width : dom . getAttribute ( "width" ) ,
76+ height : dom . getAttribute ( "height" ) ,
77+ } ) ,
78+ } ,
79+ ] ;
80+ } ,
81+ renderHTML ( { HTMLAttributes } ) {
82+ return [ "img" , HTMLAttributes ] ;
83+ } ,
84+ addMenuItems ( { editor, buttons, menu } ) {
85+ menu . defineItem ( {
86+ name : "addImage" ,
87+ groups : "link" ,
88+ command : ( editor ) => editor . chain ( ) . addImage ( ) . focus ( ) . run ( ) ,
89+ button : buttons . material ( "image" , "Add image" ) ,
90+ enabled : ( editor ) => true ,
91+ active : ( editor ) => editor . isActive ( "image" ) ,
92+ } ) ;
93+ } ,
94+ addCommands ( ) {
95+ return {
96+ ...this . parent ?. ( ) ,
97+ addImage :
98+ ( ) =>
99+ ( { editor } ) => {
100+ const attrs = editor . getAttributes ( this . name ) ;
101+
102+ ImageDialog ( editor , attrs , this . options ) . then ( ( attrs ) => {
103+ if ( attrs ) {
104+ editor
105+ . chain ( )
106+ . focus ( )
107+ . insertContent ( {
108+ type : "image" ,
109+ attrs : attrs ,
110+ } )
111+ . run ( ) ;
112+ }
113+ } ) ;
114+ } ,
115+ } ;
116+ } ,
117+ } ) ;
0 commit comments