@@ -39,56 +39,83 @@ export const defaultOptions = {
3939 disableEditing : true ,
4040}
4141
42- export const ColorPickerButton = MediumEditor . extensions . button . extend ( {
43- name : 'colorpicker' ,
44- tagNames : [ 'mark' ] ,
45- contentDefault : '<b>Color</b>' ,
46- aria : 'Color Picker' ,
47- action : 'colorPicker' ,
48- init : function ( ) {
49- rangy . init ( )
50- MediumEditor . extensions . button . prototype . init . call ( this )
51- this . colorPicker = new Picker ( {
52- parent : this . button ,
53- color : '#000' ,
54- onDone : ( res ) => {
55- if ( this . coloredText && this . coloredText . isAppliedToSelection ( ) ) {
56- this . coloredText . undoToSelection ( )
57- }
58- this . coloredText = rangy . createClassApplier ( 'colored' , {
59- elementTagName : 'span' ,
60- elementProperties : {
61- style : {
62- color : res . hex ,
63- } ,
64- } ,
65- normalize : true ,
66- } )
67- this . coloredText . toggleSelection ( )
68- this . base . checkContentChanged ( )
69- this . setInactive ( )
70- } ,
71- } )
72- } ,
73- getButton : function ( ) {
74- return this . button
75- } ,
76- handleClick : function ( ) {
77- this . setActive ( )
78- this . colorPicker . show ( )
79- } ,
80- isAlreadyApplied : function ( node ) {
81- return node . nodeName . toLowerCase ( ) === 'mark'
82- } ,
83- isActive : function ( ) {
84- return this . button . classList . contains ( 'medium-editor-button-active' )
85- } ,
86- setInactive : function ( ) {
87- this . button . classList . remove ( 'medium-editor-button-active' )
88- } ,
89- setActive : function ( ) {
90- this . button . classList . add ( 'medium-editor-button-active' )
91- } ,
92- } )
42+ export function createColorPickerButtonClass ( MediumEditor ?: any ) {
43+ const ButtonBase =
44+ MediumEditor ?. extensions ?. button || MediumEditor ?. extensions ?. button
45+ const ExtensionBase =
46+ MediumEditor ?. Extension || ( MediumEditor as any ) ?. Extension
47+
48+ // 当 Button 扩展基类不可用时,回退到 Extension 基类,避免在模块加载阶段抛错
49+ const Base = ButtonBase || ExtensionBase
50+ if ( ! Base ) {
51+ console . warn (
52+ 'MediumEditor button/extension base not available; using noop extension' ,
53+ )
54+ return class { }
55+ }
56+
57+ return Base . extend ( {
58+ name : 'colorpicker' ,
59+ tagNames : [ 'mark' ] ,
60+ contentDefault : '<b>Color</b>' ,
61+ aria : 'Color Picker' ,
62+ action : 'colorPicker' ,
63+ init : function ( ) {
64+ try {
65+ rangy . init ( )
66+ } catch {
67+ console . error ( 'rangy.init failed' )
68+ }
69+ // 初始化按钮(ButtonBase 才有 prototype.init)
70+ try {
71+ ; ( ButtonBase as any ) ?. prototype ?. init ?. call ( this )
72+ } catch {
73+ console . error ( 'ButtonBase.init failed' )
74+ }
75+ this . colorPicker = new Picker ( {
76+ parent : ( this as any ) . button || undefined ,
77+ color : '#000' ,
78+ onDone : ( res ) => {
79+ try {
80+ if ( this . coloredText && this . coloredText . isAppliedToSelection ?.( ) ) {
81+ this . coloredText . undoToSelection ( )
82+ }
83+ this . coloredText = rangy . createClassApplier ( 'colored' , {
84+ elementTagName : 'span' ,
85+ elementProperties : { style : { color : res . hex } } ,
86+ normalize : true ,
87+ } )
88+ this . coloredText . toggleSelection ( )
89+ this . base ?. checkContentChanged ?.( )
90+ this . setInactive ?.( )
91+ } catch {
92+ console . error ( 'Picker.onDone failed' )
93+ }
94+ } ,
95+ } )
96+ } ,
97+ getButton : function ( ) {
98+ return ( this as any ) . button
99+ } ,
100+ handleClick : function ( ) {
101+ this . setActive ?.( )
102+ this . colorPicker ?. show ?.( )
103+ } ,
104+ isAlreadyApplied : function ( node ) {
105+ return node ?. nodeName ?. toLowerCase ?.( ) === 'mark'
106+ } ,
107+ isActive : function ( ) {
108+ return ( this as any ) . button ?. classList ?. contains (
109+ 'medium-editor-button-active' ,
110+ )
111+ } ,
112+ setInactive : function ( ) {
113+ ; ( this as any ) . button ?. classList ?. remove ( 'medium-editor-button-active' )
114+ } ,
115+ setActive : function ( ) {
116+ ; ( this as any ) . button ?. classList ?. add ( 'medium-editor-button-active' )
117+ } ,
118+ } )
119+ }
93120
94121export { MediumEditor }
0 commit comments