Can't get draft-js Modifier's applyInlineStyle function to apply inline style
I am leveraging the draft-js and react-draft-wysiwyg libraries to create a WYSIWYG editor. I am looking to add some custom options to the toolbar to render the final HTML, such as inline line-height
. However, I cannot get the Modifier's applyInlineStyle()
function to work properly.
Editor:
import React, { Component } from "react";
import { ContentState, convertFromHTML, convertFromRaw, convertToRaw, EditorState, Modifier } from "draft-js";
import { Editor } from "react-draft-wysiwyg";
import draftToHtml from "draftjs-to-html";
import "../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
const toolbarOptions = {
options: ["inline"],
inline: {
options: ["bold", "italic", "underline"],
bold: { className: "rich-text-icon" },
italic: { className: "rich-text-icon" },
underline: { className: "rich-text-icon" }
},
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(),
finalHTML: ""
};
}
onEditorStateChange = editorState => {
const raw = convertToRaw(editorState.getCurrentContent());
const markup = draftToHtml(raw);
this.setState({
editorState,
finalHTML: markup
});
console.log(markup);
};
render() {
return (
<div>
<div className="App">
<Editor
editorState={this.state.editorState}
onEditorStateChange={this.onEditorStateChange}
toolbar={toolbarOptions}
toolbarCustomButtons={[<ApplyLineHeight />]}
stripPastedStyles={true}
/>
</div>
</div>
);
}
}
Custom line height option:
class ApplyLineHeight extends Component {
applyLineHeight = () => {
const { editorState, onChange } = this.props;
const contentState = Modifier.applyInlineStyle(editorState.getCurrentContent(), editorState.getSelection(), "line-height: 20px;");
onChange(EditorState.push(editorState, contentState, "change-inline-style"));
};
render() {
return <div onClick={this.applyLineHeight}>Change line height</div>;
}
}
When I follow the example from the docs (under "Adding new option to the toolbar"), it works with the Modifier.replaceText()
function (to replace text) but it does not work when attempting to return line-height. I am returned the same <p></p>
tags with no inline style applied. What could be causing this function to not render?
javascript reactjs
add a comment |
I am leveraging the draft-js and react-draft-wysiwyg libraries to create a WYSIWYG editor. I am looking to add some custom options to the toolbar to render the final HTML, such as inline line-height
. However, I cannot get the Modifier's applyInlineStyle()
function to work properly.
Editor:
import React, { Component } from "react";
import { ContentState, convertFromHTML, convertFromRaw, convertToRaw, EditorState, Modifier } from "draft-js";
import { Editor } from "react-draft-wysiwyg";
import draftToHtml from "draftjs-to-html";
import "../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
const toolbarOptions = {
options: ["inline"],
inline: {
options: ["bold", "italic", "underline"],
bold: { className: "rich-text-icon" },
italic: { className: "rich-text-icon" },
underline: { className: "rich-text-icon" }
},
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(),
finalHTML: ""
};
}
onEditorStateChange = editorState => {
const raw = convertToRaw(editorState.getCurrentContent());
const markup = draftToHtml(raw);
this.setState({
editorState,
finalHTML: markup
});
console.log(markup);
};
render() {
return (
<div>
<div className="App">
<Editor
editorState={this.state.editorState}
onEditorStateChange={this.onEditorStateChange}
toolbar={toolbarOptions}
toolbarCustomButtons={[<ApplyLineHeight />]}
stripPastedStyles={true}
/>
</div>
</div>
);
}
}
Custom line height option:
class ApplyLineHeight extends Component {
applyLineHeight = () => {
const { editorState, onChange } = this.props;
const contentState = Modifier.applyInlineStyle(editorState.getCurrentContent(), editorState.getSelection(), "line-height: 20px;");
onChange(EditorState.push(editorState, contentState, "change-inline-style"));
};
render() {
return <div onClick={this.applyLineHeight}>Change line height</div>;
}
}
When I follow the example from the docs (under "Adding new option to the toolbar"), it works with the Modifier.replaceText()
function (to replace text) but it does not work when attempting to return line-height. I am returned the same <p></p>
tags with no inline style applied. What could be causing this function to not render?
javascript reactjs
It seems like you are not returning 'handled' after you apply the inline style manually. If you just return 'handled' afteronChange(EditorState.push(editorState, contentState, "change-inline-style"));
, it might fix the issue. Currently, Draft is also handling the same action which might mess things up.
– quantdaddy
Dec 1 '18 at 22:35
add a comment |
I am leveraging the draft-js and react-draft-wysiwyg libraries to create a WYSIWYG editor. I am looking to add some custom options to the toolbar to render the final HTML, such as inline line-height
. However, I cannot get the Modifier's applyInlineStyle()
function to work properly.
Editor:
import React, { Component } from "react";
import { ContentState, convertFromHTML, convertFromRaw, convertToRaw, EditorState, Modifier } from "draft-js";
import { Editor } from "react-draft-wysiwyg";
import draftToHtml from "draftjs-to-html";
import "../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
const toolbarOptions = {
options: ["inline"],
inline: {
options: ["bold", "italic", "underline"],
bold: { className: "rich-text-icon" },
italic: { className: "rich-text-icon" },
underline: { className: "rich-text-icon" }
},
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(),
finalHTML: ""
};
}
onEditorStateChange = editorState => {
const raw = convertToRaw(editorState.getCurrentContent());
const markup = draftToHtml(raw);
this.setState({
editorState,
finalHTML: markup
});
console.log(markup);
};
render() {
return (
<div>
<div className="App">
<Editor
editorState={this.state.editorState}
onEditorStateChange={this.onEditorStateChange}
toolbar={toolbarOptions}
toolbarCustomButtons={[<ApplyLineHeight />]}
stripPastedStyles={true}
/>
</div>
</div>
);
}
}
Custom line height option:
class ApplyLineHeight extends Component {
applyLineHeight = () => {
const { editorState, onChange } = this.props;
const contentState = Modifier.applyInlineStyle(editorState.getCurrentContent(), editorState.getSelection(), "line-height: 20px;");
onChange(EditorState.push(editorState, contentState, "change-inline-style"));
};
render() {
return <div onClick={this.applyLineHeight}>Change line height</div>;
}
}
When I follow the example from the docs (under "Adding new option to the toolbar"), it works with the Modifier.replaceText()
function (to replace text) but it does not work when attempting to return line-height. I am returned the same <p></p>
tags with no inline style applied. What could be causing this function to not render?
javascript reactjs
I am leveraging the draft-js and react-draft-wysiwyg libraries to create a WYSIWYG editor. I am looking to add some custom options to the toolbar to render the final HTML, such as inline line-height
. However, I cannot get the Modifier's applyInlineStyle()
function to work properly.
Editor:
import React, { Component } from "react";
import { ContentState, convertFromHTML, convertFromRaw, convertToRaw, EditorState, Modifier } from "draft-js";
import { Editor } from "react-draft-wysiwyg";
import draftToHtml from "draftjs-to-html";
import "../node_modules/react-draft-wysiwyg/dist/react-draft-wysiwyg.css";
const toolbarOptions = {
options: ["inline"],
inline: {
options: ["bold", "italic", "underline"],
bold: { className: "rich-text-icon" },
italic: { className: "rich-text-icon" },
underline: { className: "rich-text-icon" }
},
};
class App extends React.Component {
constructor(props) {
super(props);
this.state = {
editorState: EditorState.createEmpty(),
finalHTML: ""
};
}
onEditorStateChange = editorState => {
const raw = convertToRaw(editorState.getCurrentContent());
const markup = draftToHtml(raw);
this.setState({
editorState,
finalHTML: markup
});
console.log(markup);
};
render() {
return (
<div>
<div className="App">
<Editor
editorState={this.state.editorState}
onEditorStateChange={this.onEditorStateChange}
toolbar={toolbarOptions}
toolbarCustomButtons={[<ApplyLineHeight />]}
stripPastedStyles={true}
/>
</div>
</div>
);
}
}
Custom line height option:
class ApplyLineHeight extends Component {
applyLineHeight = () => {
const { editorState, onChange } = this.props;
const contentState = Modifier.applyInlineStyle(editorState.getCurrentContent(), editorState.getSelection(), "line-height: 20px;");
onChange(EditorState.push(editorState, contentState, "change-inline-style"));
};
render() {
return <div onClick={this.applyLineHeight}>Change line height</div>;
}
}
When I follow the example from the docs (under "Adding new option to the toolbar"), it works with the Modifier.replaceText()
function (to replace text) but it does not work when attempting to return line-height. I am returned the same <p></p>
tags with no inline style applied. What could be causing this function to not render?
javascript reactjs
javascript reactjs
edited Nov 19 '18 at 16:47
Jiang YD
2,4391715
2,4391715
asked Nov 19 '18 at 16:23
shaunjacobsenshaunjacobsen
235515
235515
It seems like you are not returning 'handled' after you apply the inline style manually. If you just return 'handled' afteronChange(EditorState.push(editorState, contentState, "change-inline-style"));
, it might fix the issue. Currently, Draft is also handling the same action which might mess things up.
– quantdaddy
Dec 1 '18 at 22:35
add a comment |
It seems like you are not returning 'handled' after you apply the inline style manually. If you just return 'handled' afteronChange(EditorState.push(editorState, contentState, "change-inline-style"));
, it might fix the issue. Currently, Draft is also handling the same action which might mess things up.
– quantdaddy
Dec 1 '18 at 22:35
It seems like you are not returning 'handled' after you apply the inline style manually. If you just return 'handled' after
onChange(EditorState.push(editorState, contentState, "change-inline-style"));
, it might fix the issue. Currently, Draft is also handling the same action which might mess things up.– quantdaddy
Dec 1 '18 at 22:35
It seems like you are not returning 'handled' after you apply the inline style manually. If you just return 'handled' after
onChange(EditorState.push(editorState, contentState, "change-inline-style"));
, it might fix the issue. Currently, Draft is also handling the same action which might mess things up.– quantdaddy
Dec 1 '18 at 22:35
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378819%2fcant-get-draft-js-modifiers-applyinlinestyle-function-to-apply-inline-style%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53378819%2fcant-get-draft-js-modifiers-applyinlinestyle-function-to-apply-inline-style%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It seems like you are not returning 'handled' after you apply the inline style manually. If you just return 'handled' after
onChange(EditorState.push(editorState, contentState, "change-inline-style"));
, it might fix the issue. Currently, Draft is also handling the same action which might mess things up.– quantdaddy
Dec 1 '18 at 22:35