@zohodesk/dot
Version:
In this Library, we Provide Some Basic Components to Build Your Application
462 lines (398 loc) • 24.2 kB
JavaScript
;
function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
Object.defineProperty(exports, "__esModule", {
value: true
});
exports["default"] = void 0;
var _react = _interopRequireWildcard(require("react"));
var _defaultProps = require("./props/defaultProps");
var _propTypes = require("./props/propTypes");
var _selectn = _interopRequireDefault(require("selectn"));
var _Layout = require("@zohodesk/components/es/v1/Layout");
var _editorUtils = require("../../../../utils/editorUtils");
var _TextEditorModule = _interopRequireDefault(require("../../../../form/fields/TextEditor/TextEditor.module.css"));
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
/* eslint-disable camelcase */
/* eslint-disable no-undef */
/* eslint-disable react/no-deprecated */
var TextEditor = /*#__PURE__*/function (_Component) {
_inherits(TextEditor, _Component);
var _super = _createSuper(TextEditor);
function TextEditor(props) {
var _this;
_classCallCheck(this, TextEditor);
_this = _super.call(this, props);
_this.state = {
isEditorLoad: false,
isResourceLoaded: false,
isEditorShow: false
};
var methods = ['editorCallback', 'handleUpload', 'handleChange', 'focusCallback', 'handleInlineUpload', // 'handleDropUpload',
'blurCallback', 'handleGetContent', 'initializeEditor', 'clearEditorData', 'plainTextSwitchCallback', 'onWindowClick', 'toggleEditor'];
_editorUtils.bind.apply(_assertThisInitialized(_this), methods);
return _this;
}
_createClass(TextEditor, [{
key: "componentDidMount",
value: function componentDidMount() {
var _this2 = this;
this.mount = true;
if (typeof global.ZohoDeskEditor === 'undefined') {
(0, _editorUtils.loadResource)(this.props.EDITORURL, 'js', 'ZohoDeskEditor').then(function () {
_this2.setState({
isResourceLoaded: true
});
});
} else {
this.setState({
isResourceLoaded: true
});
}
window.addEventListener('click', this.onWindowClick);
}
}, {
key: "initializeEditor",
value: function initializeEditor(id, mode) {
var _this$props = this.props,
value = _this$props.value,
_this$props$editorMod = _this$props.editorMode,
editorMode = _this$props$editorMod === void 0 ? '' : _this$props$editorMod,
_this$props$editorOpt = _this$props.editorOptions,
editorOptions = _this$props$editorOpt === void 0 ? {} : _this$props$editorOpt,
_this$props$needEdito = _this$props.needEditorFocus,
needEditorFocus = _this$props$needEdito === void 0 ? true : _this$props$needEdito,
fontFamily = _this$props.fontFamily,
fontSize = _this$props.fontSize,
initCallback = _this$props.initCallback,
handleAlertMessage = _this$props.handleAlertMessage,
needInlineAttachment = _this$props.needInlineAttachment,
customInitOption = _this$props.customInitOption;
if (id && global.ZohoDeskEditor) {
var defaultObj = {
id: id,
content: (0, _editorUtils.fixEncoding)(value),
callback: this.editorCallback,
imageuploadcallback: this.handleInlineUpload,
// handleInlineDropImage: handleDropUpload ? this.handleDropUpload : undefined,
contentChanged: this.handleChange,
focusCallback: this.focusCallback,
lineHeight: 'Normal',
backgroundColor: 'transparent',
defaultfontsize: '14px',
allowTagsInsertHTML: ['iframe'],
allowAttrsInsertHTML: ['frameborder', 'allowfullscreen'],
needEditorFocus: needEditorFocus,
blurCallback: this.blurCallback,
mode: mode,
handleAlertMessage: handleAlertMessage,
plainTextSwitchCallback: this.plainTextSwitchCallback,
imgAspect: true,
handleInlineDropImage: needInlineAttachment ? this.handleInlineUpload : null,
needInlineAttachHandling: needInlineAttachment,
hookOnInsertHTML: _editorUtils.validateEditorContentOnInsertHtml,
needCustomPaste: function needCustomPaste(event, editorObj) {
return true; // By default reply editor needs customPaste, so returning true.
},
htmlVersion: '<!Doctype html>'
};
if (!customInitOption) {
if (!ZohoDeskEditor_Init.init) {
global.editorInitialize && global.editorInitialize();
}
ZohoDeskEditor_Init.init(editorMode, fontFamily, initCallback, fontSize);
}
defaultObj = Object.assign(defaultObj, editorOptions);
var globalEditor = global.ZohoDeskEditor;
globalEditor && typeof globalEditor.create == 'function' && globalEditor.create(defaultObj, customInitOption);
}
}
}, {
key: "plainTextSwitchCallback",
value: function plainTextSwitchCallback() {
var _this$props2 = this.props,
plainTextSwitchCallback = _this$props2.plainTextSwitchCallback,
id = _this$props2.id;
var content = this.handleGetContent();
plainTextSwitchCallback && plainTextSwitchCallback(content, id);
}
}, {
key: "clearEditorData",
value: function clearEditorData(id) {
var getRef = this.props.getRef;
if (global.editor && global.editor[id]) {
delete global.editor[id];
}
getRef && getRef(null);
}
}, {
key: "UNSAFE_componentWillReceiveProps",
value: function UNSAFE_componentWillReceiveProps(np) {
var changeEditorContent = this.props.changeEditorContent;
var id = np.id;
var editorObj = global.editor ? global.editor[id] ? global.editor[id] : {} : {};
if (np.isEmptyContent && editorObj) {
editorObj.setContent('');
}
if (typeof changeEditorContent !== 'undefined' && np.changeEditorContent != changeEditorContent && np.changeEditorContent) {
(0, _editorUtils.changeContent)(id, np.value);
}
}
}, {
key: "componentDidUpdate",
value: function componentDidUpdate(prevProps, prevState) {
var _this$props3 = this.props,
id = _this$props3.id,
value = _this$props3.value,
shouldUpdateContent = _this$props3.shouldUpdateContent,
mode = _this$props3.mode;
var isResourceLoaded = this.state.isResourceLoaded;
if (prevProps.id !== id) {
global.editor[id] = global.editor[prevProps.id];
delete global.editor[prevProps.id];
shouldUpdateContent && (0, _editorUtils.changeContent)(id, value);
}
if (prevState.isResourceLoaded !== isResourceLoaded && isResourceLoaded) {
this.initializeEditor(id, mode);
}
}
}, {
key: "componentWillUnmount",
value: function componentWillUnmount() {
this.mount = false;
this.clearEditorData(this.props.id);
window.removeEventListener('click', this.onWindowClick);
}
}, {
key: "focusCallback",
value: function focusCallback() {
var onFocus = this.props.onFocus;
onFocus && onFocus();
}
}, {
key: "handleGetContent",
value: function handleGetContent() {
var id = this.props.id;
var editorObj = global.editor[id] ? global.editor[id] : {};
var content = (0, _editorUtils.getRichEditorContent)(id);
if (!(0, _editorUtils.editorContentValidate)(editorObj)) {
content = '';
}
content = content.trim();
return content;
}
}, {
key: "handleChange",
value: function handleChange() {
var _this$props4 = this.props,
onChange = _this$props4.onChange,
id = _this$props4.id;
if (onChange) {
var content = this.handleGetContent();
onChange(content, id);
}
}
}, {
key: "editorCallback",
value: function editorCallback(editorObj) {
var _this$props5 = this.props,
editorCallback = _this$props5.editorCallback,
getRef = _this$props5.getRef,
isCustomScroll = _this$props5.isCustomScroll,
customCSS = _this$props5.customCSS;
this.setState({
isEditorLoad: true,
isEditorShow: true
});
var id = this.props.id;
global.editor = global.editor || {};
if (!global.editor[id]) {
global.editor[id] = editorObj;
}
editorCallback && editorCallback(editorObj);
var iframe = (0, _selectn["default"])('iframe.contentDocument.body', editorObj) || null;
var iframeHead = (0, _selectn["default"])('iframe.contentDocument.head', editorObj) || null;
getRef && getRef(iframe);
if (iframe) {
iframe.addEventListener('click', function () {
return editorObj.iframe.click();
});
if (isCustomScroll) {
var link = document.createElement('style');
link.innerText = "body{font-feature-settings: 'kern' 1, 'liga' 1, 'calt' 0}.scroll{--zd-scroll-width:12px;--zd-scroll-height:12px;--zd-scroll-bg:rgba(255,255,255,0.1);--zd-scroll-corner-bg:transparent;--zd-scroll-thump:rgba(44,51,77,0.2);--zd-scroll-thump-hoverbg:rgba(44,51,77,0.3);--zd-scroll-border:rgba(44,51,77,0.1)}.scroll[data-scroll-palette='dark'],.scroll [data-scroll-palette='dark']{--zd-scroll-thump:rgba(255,255,255,0.2);--zd-scroll-thump-hoverbg:rgba(255,255,255,0.4);--zd-scroll-border:rgba(255,255,255,0.07)}.scroll,.scroll *{scrollbar-color:var(--zd-scroll-thump) var(--zd-scroll-bg);scrollbar-width:thin;-ms-scrollbar-highlight-color:var(--zd-scroll-bg);-ms-scrollbar-face-color:var(--zd-scroll-thump)}.scroll::-webkit-scrollbar,.scroll ::-webkit-scrollbar{background:var(--zd-scroll-corner-bg)}.scroll::-webkit-scrollbar:hover,.scroll ::-webkit-scrollbar:hover{background:var(--zd-scroll-bg)}.scroll::-webkit-scrollbar:horizontal,.scroll ::-webkit-scrollbar:horizontal{height:var(--zd-scroll-height)}.scroll::-webkit-scrollbar:vertical,.scroll ::-webkit-scrollbar:vertical{width:var(--zd-scroll-width)}.scroll::-webkit-scrollbar-button,.scroll ::-webkit-scrollbar-button{display:none;width:0;height:0}.scroll::-webkit-scrollbar-track:vertical,.scroll ::-webkit-scrollbar-track:vertical{border-left:1px solid transparent;border-right:1px solid transparent}.scroll::-webkit-scrollbar-track:vertical:hover,.scroll ::-webkit-scrollbar-track:vertical:hover{border-color:var(--zd-scroll-border)}.scroll::-webkit-scrollbar-track:horizontal,.scroll ::-webkit-scrollbar-track:horizontal{border-top:1px solid transparent;border-bottom:1px solid transparent}.scroll::-webkit-scrollbar-track:horizontal:hover,.scroll ::-webkit-scrollbar-track:horizontal:hover{border-color:var(--zd-scroll-border)}.scroll::-webkit-scrollbar-track-piece,.scroll ::-webkit-scrollbar-track-piece{background:inherit}.scroll::-webkit-scrollbar-thumb,.scroll ::-webkit-scrollbar-thumb{border-radius:10px;background:var(--zd-scroll-thump);background-clip:padding-box;border:3px solid transparent}.scroll::-webkit-scrollbar-thumb:hover,.scroll ::-webkit-scrollbar-thumb:hover{background:var(--zd-scroll-thump-hoverbg);background-clip:padding-box;border:3px solid transparent}.scroll::-webkit-scrollbar-corner,.scroll ::-webkit-scrollbar-corner{background:var(--zd-scroll-corner-bg)}.scroll::-webkit-resizer,.scroll::-webkit-resizer{background:inherit}";
iframeHead.appendChild(link);
iframe.classList.add('scroll');
}
var customStyleTag = document.createElement('style');
customStyleTag.type = 'text/css';
var customizedCSS = "pre {white-space: pre-wrap} ".concat(customCSS);
var fontCSS = "@font-face {font-family: 'ZohoPuviRegular';src: url('https://static.zohocdn.com/zohofonts/zohopuvi/4.0/Zoho_Puvi_Regular.woff2') format('woff2');font-style: normal;font-weight: 400;text-rendering: optimizeLegibility;font-display: swap}@font-face {font-family: Regular;src: url('https://static.zohocdn.com/webfonts/lato2regular/font.woff2') format('woff2');font-weight: 400;font-style: normal;font-display: swap}@font-face {font-family: 'RobotoRegular';font-weight: 400;font-style: normal;font-display: swap;src: url('https://static.zohocdn.com/webfonts/robotoregular/font.woff2')}\n @font-face {font-family: 'ZohoPuvi';src: url('https://static.zohocdn.com/zohofonts/zohopuvi/4.0/Zoho_Puvi_Regular.woff2') format('woff2');font-style: normal;font-weight: 400;text-rendering: optimizeLegibility;font-display: swap}@font-face {font-family: ZDLato;src: url('https://static.zohocdn.com/webfonts/lato2regular/font.woff2') format('woff2');font-weight: 400;font-style: normal;font-display: swap}@font-face {font-family: 'Roboto';font-weight: 400;font-style: normal;font-display: swap;src: url('https://static.zohocdn.com/webfonts/robotoregular/font.woff2')}";
customStyleTag.textContent = "".concat(customizedCSS, " ").concat(fontCSS);
iframeHead.appendChild(customStyleTag);
}
}
}, {
key: "handleInlineUpload",
value: function handleInlineUpload(file, successCallback, failureCallback, mode, editorObj) {
var _this$props6 = this.props,
id = _this$props6.id,
progress = _this$props6.setInlineAttachmentInProgress,
i18nKeys = _this$props6.i18nKeys,
inlineImageProps = _this$props6.inlineImageProps;
var fileUploadError = i18nKeys.fileUploadError,
fileNotSupported = i18nKeys.fileNotSupported,
fileSizeLimitExceed = i18nKeys.fileSizeLimitExceed,
fileEmptyError = i18nKeys.fileEmptyError,
fileNameLimitExceed = i18nKeys.fileNameLimitExceed;
var _inlineImageProps$all = inlineImageProps.allowedFileExtensions,
allowedFileExtensions = _inlineImageProps$all === void 0 ? ["jpeg", "jpg", "png", "gif", "bmp", "ico"] : _inlineImageProps$all,
_inlineImageProps$fil = inlineImageProps.fileNameMaxCharacters,
fileNameMaxCharacters = _inlineImageProps$fil === void 0 ? 200 : _inlineImageProps$fil,
_inlineImageProps$fil2 = inlineImageProps.fileSizeMaxBytes,
fileSizeMaxBytes = _inlineImageProps$fil2 === void 0 ? 3 * 1024 * 1024 : _inlineImageProps$fil2;
if (!(0, _editorUtils.isFileNameSizeInvalid)(file.name, fileNameMaxCharacters)) {
if (file.size != 0) {
if (file.size <= fileSizeMaxBytes) {
if ((0, _editorUtils.isImageFormat)(file.name, allowedFileExtensions)) {
progress && progress(true);
this.handleUpload(file).then(function (res) {
successCallback(res);
progress && progress(false);
if (id && editorObj) {
(0, _editorUtils.triggerKeyup)(editorObj.doc);
}
}, function () {
failureCallback(fileUploadError);
});
} else {
failureCallback(fileNotSupported);
}
} else {
failureCallback(fileSizeLimitExceed);
}
} else {
failureCallback(fileEmptyError);
}
} else {
failureCallback(fileNameLimitExceed);
}
} // handleDropUpload(file, successCallback, failureCallback) {
// let e = { target: { files: [file] } };
// let { handleDropUpload } = this.props;
// handleDropUpload(e).then(
// (res) => {
// successCallback(res);
// },
// () => {
// failureCallback();
// }
// );
// }
}, {
key: "blurCallback",
value: function blurCallback() {
var blurCallback = this.props.blurCallback;
var content = this.handleGetContent();
blurCallback && blurCallback(content);
}
}, {
key: "handleUpload",
value: function handleUpload(file) {
var _this$props7 = this.props,
onUpload = _this$props7.onUpload,
editorMode = _this$props7.editorMode,
getCSRFCookieName = _this$props7.getCSRFCookieName,
getCSRFCookie = _this$props7.getCSRFCookie;
try {
var formData = new FormData();
formData.append('img_file', file, file.name);
if (getCSRFCookieName && getCSRFCookie) {
formData.append(getCSRFCookieName(), getCSRFCookie());
}
return onUpload(formData, file);
} catch (err) {
return Promise.reject();
}
}
}, {
key: "onWindowClick",
value: function onWindowClick(e) {
var isEditorDefaultOpen = this.props.isEditorDefaultOpen;
var element = e.target;
if (!element.closest('[data-isEditor]') && !element.closest("[id='KB_Editor_UploadImage_Popup']") && !element.closest("[id='KB_Editor_Overlay']") && !element.closest('.zdeskEditor_popup') && element && typeof element.className === 'string' && !element.className.startsWith('KB') && !element.hasAttribute('data-insertoptions') && !element.closest('.KB_Editor_common_DropDown')) {
var _this$props8 = this.props,
_this$props8$value = _this$props8.value,
value = _this$props8$value === void 0 ? '' : _this$props8$value,
id = _this$props8.id;
var cont = value;
if (typeof editor !== 'undefined') {
cont = editor['addFormEditor'] && editor['addFormEditor'].getContent();
}
if (!isEditorDefaultOpen) {
if ((cont == '' || cont == '<div><br></div>') && this.mount) {
this.toggleEditor(false);
}
}
}
}
}, {
key: "toggleEditor",
value: function toggleEditor(value) {
var toggleEdit = this.props.toggleEdit;
toggleEdit && toggleEdit(value);
}
}, {
key: "render",
value: function render() {
var _this$props9 = this.props,
id = _this$props9.id,
dataId = _this$props9.dataId,
type = _this$props9.type,
border = _this$props9.border,
ImgLazyLoad = _this$props9.ImgLazyLoad,
loadingComponent = _this$props9.loadingComponent,
isNightMode = _this$props9.isNightMode,
isReadOnly = _this$props9.isReadOnly,
dataSelectorId = _this$props9.dataSelectorId,
isDisabled = _this$props9.isDisabled;
var _this$state = this.state,
isEditorLoad = _this$state.isEditorLoad,
isEditorShow = _this$state.isEditorShow;
var editorDoc = ImgLazyLoad && global.editor && global.editor[id] ? global.editor[id].doc : null;
return /*#__PURE__*/_react["default"].createElement(_Layout.Container, {
className: "".concat(_TextEditorModule["default"].textEditor, " ").concat(_TextEditorModule["default"][type], " ").concat(_TextEditorModule["default"][border], "\n ").concat(isNightMode ? _TextEditorModule["default"].night : _TextEditorModule["default"].light, " ").concat(isReadOnly ? _TextEditorModule["default"].readOnly : '', " ").concat(isReadOnly || isDisabled ? _TextEditorModule["default"].disable : '', " ").concat(!isEditorShow ? _TextEditorModule["default"].editorHide : ''),
dataId: dataId,
"data-isEditor": true,
dataSelectorId: dataSelectorId
}, loadingComponent && !isEditorLoad ? loadingComponent : null, /*#__PURE__*/_react["default"].createElement(_Layout.Box, {
flexible: true,
id: id,
dataId: dataId
}), editorDoc && ImgLazyLoad ? /*#__PURE__*/_react["default"].createElement(ImgLazyLoad, {
node: editorDoc
}) : null, isReadOnly || isDisabled ? /*#__PURE__*/_react["default"].createElement("div", {
className: _TextEditorModule["default"].readOnlyMask
}) : null);
}
}]);
return TextEditor;
}(_react.Component);
exports["default"] = TextEditor;
TextEditor.defaultProps = _defaultProps.defaultProps;
TextEditor.propTypes = _propTypes.propTypes; // if (__DOCS__) {
// TextEditor.docs = {
// componentGroup: 'Fields',
// folderName: 'TextEditor'
// };
// }