|
1 |
| -import { useEffect, useId, useState } from 'react'; |
| 1 | +import React, { useEffect, useId, useState } from 'react'; |
| 2 | +import * as ReactDOM from 'react-dom/client'; |
2 | 3 | import $ from 'jquery';
|
3 |
| -import { SummernoteProps, SummernoteContext, SummernotePluginInterface, SummernoteCallbackInitProps } from '../types'; |
| 4 | +import { |
| 5 | + SummernoteProps, |
| 6 | + SummernoteContext, |
| 7 | + SummernotePluginInterface, |
| 8 | + SummernoteCallbackInitProps, |
| 9 | + SummernoteCustomButtonProps, |
| 10 | +} from '../types'; |
4 | 11 |
|
5 | 12 | export type SummernoteEventNames =
|
6 | 13 | | 'init'
|
@@ -56,6 +63,44 @@ export function createSummernotePlugin(name: string, PluginClass: SummernotePlug
|
56 | 63 | });
|
57 | 64 | }
|
58 | 65 |
|
| 66 | +interface ButtonProps { |
| 67 | + container?: string; |
| 68 | + tooltip?: string; |
| 69 | + contents?: string; |
| 70 | + click?: () => void; |
| 71 | +} |
| 72 | + |
| 73 | +export function createSummernoteButton(opt: SummernoteCustomButtonProps): any { |
| 74 | + return (context: SummernoteContext) => { |
| 75 | + let buttonProps: ButtonProps = { |
| 76 | + container: opt.container || 'body', |
| 77 | + tooltip: opt.tooltip || 'sample', |
| 78 | + }; |
| 79 | + |
| 80 | + if (opt.title && !opt.element && !opt.render) { |
| 81 | + buttonProps.contents = opt.title; |
| 82 | + buttonProps.click = () => { |
| 83 | + opt?.onClick?.(context); |
| 84 | + }; |
| 85 | + return context.ui.button(buttonProps).render(); |
| 86 | + } else { |
| 87 | + const button = context.ui.button(buttonProps); |
| 88 | + const el = button.render(); // return button as jquery object |
| 89 | + const props = { context, ...opt.props }; |
| 90 | + |
| 91 | + if (opt?.render) { |
| 92 | + ReactDOM.createRoot(el[0]).render(opt.render(props)); |
| 93 | + } |
| 94 | + |
| 95 | + if (opt?.element && React.isValidElement(React.createElement(opt.element, props))) { |
| 96 | + ReactDOM.createRoot(el[0]).render(React.createElement(opt.element, props)); |
| 97 | + } |
| 98 | + |
| 99 | + return el; |
| 100 | + } |
| 101 | + }; |
| 102 | +} |
| 103 | + |
59 | 104 | export function setSummernoteLang(langInfo: any) {
|
60 | 105 | const jQuery = $ as any;
|
61 | 106 |
|
|
0 commit comments