Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #3097: Editor allow loading from script #3098

Merged
merged 1 commit into from
Jul 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 70 additions & 53 deletions components/lib/editor/Editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import * as React from 'react';
import { useMountEffect, useUpdateEffect } from '../hooks/Hooks';
import { classNames, DomHandler, ObjectUtils } from '../utils/Utils';

const QuillJS = function() {try {return Quill;} catch {return null;}}();

export const Editor = React.memo(React.forwardRef((props, ref) => {
const elementRef = React.useRef(null);
const contentRef = React.useRef(null);
Expand All @@ -11,69 +13,84 @@ export const Editor = React.memo(React.forwardRef((props, ref) => {

useMountEffect(() => {
if (!isQuillLoaded.current) {
import('quill').then((module) => {
if (module && DomHandler.isExist(contentRef.current)) {
const configuration = {
modules: {
toolbar: props.showHeader ? toolbarRef.current : false,
...props.modules
},
placeholder: props.placeholder,
readOnly: props.readOnly,
theme: props.theme,
formats: props.formats
};

if (module.default) {
// webpack
quill.current = new module.default(contentRef.current, configuration);
} else {
// parceljs
quill.current = new module(contentRef.current, configuration);
}
const configuration = {
modules: {
toolbar: props.showHeader ? toolbarRef.current : false,
...props.modules
},
placeholder: props.placeholder,
readOnly: props.readOnly,
theme: props.theme,
formats: props.formats
};

if (props.value) {
quill.current.setContents(quill.current.clipboard.convert(props.value));
}

quill.current.on('text-change', (delta, source) => {
let firstChild = contentRef.current.children[0];
let html = firstChild ? firstChild.innerHTML : null;
let text = quill.current.getText();
if (html === '<p><br></p>') {
html = null;
}
if (QuillJS) {
// GitHub #3097 loaded by script only
quill.current = new Quill(contentRef.current, configuration);
initQuill();

if (props.onTextChange) {
props.onTextChange({
htmlValue: html,
textValue: text,
delta: delta,
source: source
});
}
});

quill.current.on('selection-change', (range, oldRange, source) => {
if (props.onSelectionChange) {
props.onSelectionChange({
range: range,
oldRange: oldRange,
source: source
});
}
});
}
}).then(() => {
if (quill.current && quill.current.getModule('toolbar')) {
props.onLoad && props.onLoad(quill.current);
}
});
}
else {
import('quill').then((module) => {
if (module && DomHandler.isExist(contentRef.current)) {
if (module.default) {
// webpack
quill.current = new module.default(contentRef.current, configuration);
} else {
// parceljs
quill.current = new module(contentRef.current, configuration);
}

initQuill();
}
}).then(() => {
if (quill.current && quill.current.getModule('toolbar')) {
props.onLoad && props.onLoad(quill.current);
}
});
}

isQuillLoaded.current = true;
}
});

const initQuill = () => {
if (props.value) {
quill.current.setContents(quill.current.clipboard.convert(props.value));
}

quill.current.on('text-change', (delta, source) => {
let firstChild = contentRef.current.children[0];
let html = firstChild ? firstChild.innerHTML : null;
let text = quill.current.getText();
if (html === '<p><br></p>') {
html = null;
}

if (props.onTextChange) {
props.onTextChange({
htmlValue: html,
textValue: text,
delta: delta,
source: source
});
}
});

quill.current.on('selection-change', (range, oldRange, source) => {
if (props.onSelectionChange) {
props.onSelectionChange({
range: range,
oldRange: oldRange,
source: source
});
}
});
}

useUpdateEffect(() => {
if (quill.current && !quill.current.hasFocus()) {
props.value ?
Expand Down