UNPKG

zmp-react

Version:

Build full featured iOS & Android apps using ZMP & React

133 lines (117 loc) 4.01 kB
import _extends from "@babel/runtime/helpers/extends"; import React, { forwardRef, useRef, useImperativeHandle } from 'react'; import { useIsomorphicLayoutEffect } from '../shared/use-isomorphic-layout-effect'; import { classNames, getExtraAttrs, getSlots, noUndefinedProps, emit } from '../shared/utils'; import { colorClasses } from '../shared/mixins'; import { zmpready, zmp } from '../shared/zmp'; import { watchProp } from '../shared/watch-prop'; var TextEditor = /*#__PURE__*/forwardRef(function (props, ref) { var _zmpTextEditor = useRef(null); var className = props.className, id = props.id, style = props.style, mode = props.mode, value = props.value, buttons = props.buttons, customButtons = props.customButtons, dividers = props.dividers, imageUrlText = props.imageUrlText, linkUrlText = props.linkUrlText, placeholder = props.placeholder, clearFormattingOnPaste = props.clearFormattingOnPaste, _props$resizable = props.resizable, resizable = _props$resizable === void 0 ? false : _props$resizable; var extraAttrs = getExtraAttrs(props); var elRef = useRef(null); var onChange = function onChange(editor, editorValue) { emit(props, 'textEditorChange', editorValue); }; var onInput = function onInput(editor, editorValue) { emit(props, 'textEditorInput', editorValue); }; var onFocus = function onFocus() { emit(props, 'textEditorFocus'); }; var onBlur = function onBlur() { emit(props, 'textEditorBlur'); }; var onButtonClick = function onButtonClick(editor, button) { emit(props, 'textEditorButtonClick', button); }; var onKeyboardOpen = function onKeyboardOpen() { emit(props, 'textEditorKeyboardOpen'); }; var onKeyboardClose = function onKeyboardClose() { emit(props, 'textEditorKeyboardClose'); }; var onPopoverOpen = function onPopoverOpen() { emit(props, 'textEditorPopoverOpen'); }; var onPopoverClose = function onPopoverClose() { emit(props, 'textEditorPopoverClose'); }; useImperativeHandle(ref, function () { return { el: elRef.current, zmpTextEditor: function zmpTextEditor() { return _zmpTextEditor.current; } }; }); watchProp(value, function (newValue) { if (_zmpTextEditor.current) { _zmpTextEditor.current.setValue(newValue); } }); var onMount = function onMount() { var params = noUndefinedProps({ el: elRef.current, mode: mode, value: value, buttons: buttons, customButtons: customButtons, dividers: dividers, imageUrlText: imageUrlText, linkUrlText: linkUrlText, placeholder: placeholder, clearFormattingOnPaste: clearFormattingOnPaste, on: { change: onChange, input: onInput, focus: onFocus, blur: onBlur, buttonClick: onButtonClick, keyboardOpen: onKeyboardOpen, keyboardClose: onKeyboardClose, popoverOpen: onPopoverOpen, popoverClose: onPopoverClose } }); zmpready(function () { _zmpTextEditor.current = zmp.textEditor.create(params); }); }; var onDestroy = function onDestroy() { if (_zmpTextEditor.current && _zmpTextEditor.current.destroy) { _zmpTextEditor.current.destroy(); } _zmpTextEditor.current = null; }; useIsomorphicLayoutEffect(function () { onMount(); return onDestroy; }, []); var slots = getSlots(props); var classes = classNames(className, 'text-editor', resizable && 'text-editor-resizable', colorClasses(props)); return /*#__PURE__*/React.createElement("div", _extends({ ref: elRef, id: id, style: style, className: classes }, extraAttrs), slots['root-start'], /*#__PURE__*/React.createElement("div", { className: "text-editor-content", contentEditable: true }, slots.default), slots['root-end'], slots.root); }); TextEditor.displayName = 'zmp-text-editor'; export default TextEditor;