react-native-cn-quill
Version:
react-native quill richtext editor
418 lines (351 loc) • 11.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var React = _interopRequireWildcard(require("react"));
var _reactNativeWebview = require("react-native-webview");
var _reactNative = require("react-native");
var _editorUtils = require("../utils/editor-utils");
var _loading = require("./loading");
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (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 _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
class QuillEditor extends React.Component {
constructor(_props) {
super(_props);
_defineProperty(this, "_webview", void 0);
_defineProperty(this, "_handlers", void 0);
_defineProperty(this, "_promises", void 0);
_defineProperty(this, "getInitalHtml", () => {
var _quill$modules;
const {
initialHtml = '',
import3rdParties = 'local',
containerId = 'standalone-container',
theme = {
background: 'white',
color: 'rgb(32, 35, 42)',
placeholder: 'rgba(0,0,0,0.6)'
},
quill = {
id: 'editor-container',
placeholder: 'write here!',
modules: {
toolbar: false
},
theme: 'snow'
},
customFonts = [],
customStyles = [],
defaultFontFamily = undefined,
customJS = ''
} = this.props;
return (0, _editorUtils.createHtml)({
initialHtml,
placeholder: quill.placeholder,
theme: quill.theme ? quill.theme : 'snow',
toolbar: JSON.stringify((_quill$modules = quill.modules) === null || _quill$modules === void 0 ? void 0 : _quill$modules.toolbar),
libraries: import3rdParties,
editorId: quill.id ? quill.id : 'editor-container',
defaultFontFamily,
containerId,
color: theme.color,
fonts: customFonts,
backgroundColor: theme.background,
placeholderColor: theme.placeholder,
customStyles,
customJS
});
});
_defineProperty(this, "post", obj => {
var _this$_webview$curren;
const jsonString = JSON.stringify(obj);
(_this$_webview$curren = this._webview.current) === null || _this$_webview$curren === void 0 ? void 0 : _this$_webview$curren.postMessage(jsonString);
});
_defineProperty(this, "toMessage", data => {
const message = JSON.parse(data);
return message;
});
_defineProperty(this, "onMessage", event => {
const message = this.toMessage(event.nativeEvent.data);
const response = message.key ? this._promises.find(x => x.key === message.key) : undefined;
switch (message.type) {
case 'format-change':
case 'text-change':
case 'selection-change':
case 'html-change':
case 'editor-change':
case 'blur':
case 'focus':
this._handlers.filter(x => x.event === message.type).forEach(item => item.handler(message.data));
break;
case 'has-focus':
case 'get-contents':
case 'get-text':
case 'get-length':
case 'get-bounds':
case 'get-selection':
case 'get-html':
if (response) {
response.resolve(message.data);
this._promises = this._promises.filter(x => x.key !== message.key);
}
break;
}
});
_defineProperty(this, "blur", () => {
this.post({
command: 'blur'
});
});
_defineProperty(this, "focus", () => {
this.post({
command: 'focus'
});
});
_defineProperty(this, "hasFocus", () => {
return this.postAwait({
command: 'hasFocus'
});
});
_defineProperty(this, "enable", (enable = true) => {
this.post({
command: 'enable',
value: enable
});
});
_defineProperty(this, "disable", () => {
this.post({
command: 'enable',
value: false
});
});
_defineProperty(this, "update", () => {
this.post({
command: 'update'
});
});
_defineProperty(this, "format", (name, value) => {
this.post({
command: 'format',
name,
value
});
});
_defineProperty(this, "deleteText", (index, length) => {
this.post({
command: 'deleteText',
index,
length
});
});
_defineProperty(this, "getContents", (index, length) => {
return this.postAwait({
command: 'getContents',
index,
length
});
});
_defineProperty(this, "getHtml", () => {
return this.postAwait({
command: 'getHtml'
});
});
_defineProperty(this, "getLength", () => {
return this.postAwait({
command: 'getLength'
});
});
_defineProperty(this, "getText", (index, length) => {
return this.postAwait({
command: 'getText',
index,
length
});
});
_defineProperty(this, "getBounds", (index, length) => {
return this.postAwait({
command: 'getBounds',
index,
length
});
});
_defineProperty(this, "getSelection", (focus = false) => {
return this.postAwait({
command: 'getSelection',
focus
});
});
_defineProperty(this, "setSelection", (index, length, source) => {
this.post({
command: 'setSelection',
index,
length,
source
});
});
_defineProperty(this, "insertEmbed", (index, type, value) => {
this.post({
command: 'insertEmbed',
index,
type,
value
});
});
_defineProperty(this, "insertText", (index, text, formats) => {
this.post({
command: 'insertText',
index,
text,
formats
});
});
_defineProperty(this, "setContents", delta => {
this.post({
command: 'setContents',
delta
});
});
_defineProperty(this, "setText", text => {
this.post({
command: 'setText',
text
});
});
_defineProperty(this, "updateContents", delta => {
this.post({
command: 'updateContents',
delta
});
});
_defineProperty(this, "on", (event, handler) => {
this._handlers.push({
event,
handler
});
});
_defineProperty(this, "off", (event, handler) => {
const index = this._handlers.findIndex(x => x.event === event && x.handler === handler);
if (index > -1) {
this._handlers.splice(index, 1);
}
});
_defineProperty(this, "dangerouslyPasteHTML", (index, html) => {
this.post({
command: 'dangerouslyPasteHTML',
index,
html
});
});
_defineProperty(this, "renderWebview", (content, style, props = {}) => /*#__PURE__*/React.createElement(_reactNativeWebview.WebView, _extends({
scrollEnabled: false,
hideKeyboardAccessoryView: true,
keyboardDisplayRequiresUserAction: false,
originWhitelist: ['*'],
style: style,
onError: syntheticEvent => {
const {
nativeEvent
} = syntheticEvent;
console.warn('WebView error: ', nativeEvent);
},
allowFileAccess: true,
domStorageEnabled: false,
automaticallyAdjustContentInsets: true,
bounces: false,
dataDetectorTypes: "none"
}, props, {
javaScriptEnabled: true,
source: {
html: content
},
ref: this._webview,
onMessage: this.onMessage
})));
this._webview = /*#__PURE__*/React.createRef();
this.state = {
webviewContent: this.getInitalHtml()
};
this._handlers = [];
this._promises = [];
const {
onSelectionChange,
onEditorChange,
onTextChange,
onHtmlChange,
onBlur,
onFocus
} = this.props;
if (onSelectionChange) {
this.on('selection-change', onSelectionChange);
}
if (onEditorChange) {
this.on('editor-change', onEditorChange);
}
if (onTextChange) {
this.on('text-change', onTextChange);
}
if (onHtmlChange) {
this.on('html-change', onHtmlChange);
}
if (onBlur) {
this.on('blur', onBlur);
}
if (onFocus) {
this.on('focus', onFocus);
}
}
getKey() {
var timestamp = new Date().getUTCMilliseconds();
return `${timestamp}${Math.random()}`;
}
postAwait(data) {
const key = this.getKey();
let resolveFn;
resolveFn = () => {};
const promise = new Promise(resolve => {
resolveFn = resolve;
});
const resp = {
key,
resolve: resolveFn
};
this._promises.push(resp);
this.post({ ...data,
key
});
return promise;
}
render() {
const {
webviewContent
} = this.state;
const {
style,
webview,
container = false,
loading = 'Please Wait ...'
} = this.props;
if (container === false) {
if (!webviewContent) return /*#__PURE__*/React.createElement(_reactNative.Text, null, "Please wait...");
return this.renderWebview(webviewContent, style, webview);
} else {
const ContainerComponent = container === true ? _reactNative.View : container;
return /*#__PURE__*/React.createElement(ContainerComponent, {
style: style
}, webviewContent ? this.renderWebview(webviewContent, styles.webView, webview) : typeof loading === 'string' ? /*#__PURE__*/React.createElement(_loading.Loading, {
text: loading
}) : loading);
}
}
}
exports.default = QuillEditor;
let styles = _reactNative.StyleSheet.create({
webView: {
flexGrow: 1,
borderWidth: 0
}
});
//# sourceMappingURL=quill-editor.js.map