react-draft-wysiwyg icon indicating copy to clipboard operation
react-draft-wysiwyg copied to clipboard

How can I set the default fontSize

Open Imhemers opened this issue 7 years ago • 23 comments

It seems that there is no API to set up

fontSize: { defaultSize: 18,//This does not work. options: [10,12, 14, 16, 18, 24, 30, 36, 48, 60], },

Imhemers avatar Dec 13 '17 06:12 Imhemers

This can be done using editorStyle or editorClassName. Simply setting css font-size will change font-size of the editor.

jpuri avatar Dec 18 '17 14:12 jpuri

Hi @jpuri your solution only changes what is displayed in the editor. Is there a way to set the default font size of the draft js content?

shakdoesgithub avatar Jan 11 '18 07:01 shakdoesgithub

Good point, its missed. Atm you can do that by selecting font-size from dropdown.

This default font-size information is required to be added to all the blocks, I will try to fix this.

jpuri avatar Jan 15 '18 16:01 jpuri

Default font-size can be changed by using editorStyle or editorClassName. Trouble atm is that its not saved in editor-content.

A work around could set default size of font while displaying editor content next time also.

jpuri avatar Jan 23 '18 18:01 jpuri

Is this fixed??

Pratapvirendra47 avatar Aug 16 '18 13:08 Pratapvirendra47

Hi @jpuri any updates on this? I have same issue. Version: 1.12.13 Setting font size through editorStyle / editorClassName only sets content font size. Does not change the default font size in the dropdown. Hope for a fix soon, Thanks!

mandarspringct avatar Sep 18 '18 12:09 mandarspringct

Hello, also wondering if this is going to be available as an option soon?

shaunjacobsen avatar Nov 29 '18 20:11 shaunjacobsen

Hello @jpuri please update the status of this issue.

sachinkammar avatar Jan 30 '19 09:01 sachinkammar

Hey @sachinkammar : defaultFont size can be set as described here: https://github.com/jpuri/react-draft-wysiwyg/issues/551#issuecomment-352435105

I still need to add it to options.

jpuri avatar Feb 01 '19 06:02 jpuri

Similarly, users need to be able to set the default fontFamily. Currently setting the font family will change the display, but not the actual content of the editor.

jakeleventhal avatar Mar 02 '19 19:03 jakeleventhal

Any update?

sandropoluan avatar May 25 '19 22:05 sandropoluan

I need this feature

sandropoluan avatar May 25 '19 22:05 sandropoluan

I've managed to get a working fix for this issue, but it's fairly convoluted.

constructor(props) {
  ...
  const fontFamily = `fontfamily-${DEFAULT_FONT_FAMILY}`;
  const fontSize = `fontsize-${DEFAULT_FONT_SIZE}`;
 ...
  this.state = {
    editorState,
    fontFamily,
    fontSize,
  };
}

...

setDefaultFontFamilyAndSize = (editorState) => {
  const currentStyles = editorState.getCurrentInlineStyle();

  let newEditorState = editorState;
  if (!currentStyles.some(style => style.match(/^fontfamily-/))) {
    newEditorState = RichUtils.toggleInlineStyle(
      newEditorState,
      this.state.fontFamily,
    );
  }
  if (!currentStyles.some(style => style.match(/^fontsize-/))) {
    newEditorState = RichUtils.toggleInlineStyle(
      newEditorState,
      this.state.fontSize,
    );
  }

  return newEditorState;
}

...

ensureOnlyOneFontSizeAndFamily = (editorState) => {
  const currentStyles = editorState.getCurrentInlineStyle();
  const fontFamilies = currentStyles.filter(style => style.match(/^fontfamily-/));
  const fontSizes = currentStyles.filter(style => style.match(/^fontsize-/));
  let newEditorState = editorState;

  if (fontFamilies.size > 1) {
    newEditorState = RichUtils.toggleInlineStyle(
      newEditorState,
      fontFamilies.first(),
    );
  }
  if (fontSizes.size > 1) {
    newEditorState = RichUtils.toggleInlineStyle(
      newEditorState,
      fontSizes.first(),
    );
  }

  return newEditorState;
}

...

onEditorChange = (editorState) => {
  ...
  const currentStyles = editorState.getCurrentInlineStyle();
  const fontFamily = currentStyles.filter(style => style.match(/^fontfamily-/)).last();
  const fontSize = currentStyles.filter(style => style.match(/^fontsize-/)).last();
  if (fontFamily) this.setState({ fontFamily });
  if (fontSize) this.setState({ fontSize });

  let newEditorState = this.ensureOnlyOneFontSizeAndFamily(editorState);
  newEditorState = this.setDefaultFontFamilyAndSize(newEditorState);
  ...
}

...

componentDidMount() {
  const editorState = this.setDefaultFontFamilyAndSize(this.state.editorState);
  this.onEditorChange(editorState);
}

It would be far easier were the API to reveal an option for a default fontSize/Family, of course, but in the absence of this, this is doing the job for me.

mikeappell avatar Jun 20 '19 17:06 mikeappell

Is there a way to add a font family and keep all the other toolbar options

SangeetAgarwal avatar Apr 04 '20 01:04 SangeetAgarwal

It seems as though setting editorStyle to something like fontSize: 16 sets the default selected font size to 16 (not just what is displayed). Tested on version 1.14.7

ledesmablt avatar Sep 28 '21 05:09 ledesmablt

I am using react-final-form i am making crud app every thing working fine but when i do edit with the help of this editor, first time it work perfect but when i again edit the same task it does not show the previous style in editor box <Field component={DraftEditor} name="description" type="text" />

`/* eslint-disable no-underscore-dangle / / eslint-disable react/destructuring-assignment / / eslint-disable max-len / / eslint-disable no-undef / / eslint-disable react/prop-types / / eslint-disable no-unused-vars / / eslint-disable no-shadow */ import * as React from 'react'; import { useState } from 'react'; import { EditorState, convertToRaw, ContentState, convertFromHTML, Modifier, editorRef, } from 'draft-js'; import { stateFromHTML } from 'draft-js-import-html'; import { Editor } from 'react-draft-wysiwyg'; import draftToHtml from 'draftjs-to-html'; import htmlToDraft from 'html-to-draftjs'; import parser, { processNodes, convertNodeToElement, htmlparser2 } from 'react-html-parser';

const WYSIWYGEditor = ({ input, meta }) => { const blocksFromHTML = convertFromHTML(input.value); const content = ContentState.createFromBlockArray( blocksFromHTML.contentBlocks, blocksFromHTML.entityMap, ); const raw = convertToRaw(content); const [contentState, setContentState] = useState(raw); const [editorState, setEditorState] = useState(EditorState.createEmpty());

const onEditorStateChange = (contentState) => { setEditorState(contentState); return input.onChange(draftToHtml(convertToRaw(editorState.getCurrentContent()))); }; return ( <div className="editor"> <Editor defaultContentState={contentState} onEditorStateChange={onEditorStateChange} wrapperClassName="wrapper-class" editorClassName="editor-class" toolbarClassName="toolbar-class" placeholder="Write your answere here" toolbar={{ options: [ 'inline', 'blockType', 'fontSize', 'fontFamily', 'colorPicker', ], inline: { options: ['bold', 'italic', 'underline'], }, fontSize: { options: [8, 9, 10, 11, 12, 14, 16, 18, 24, 30, 36, 48, 60, 72, 96], className: undefined, component: undefined, dropdownClassName: undefined, }, blockType: { options: ['Normal', 'H1', 'H2', 'H3', 'H4', 'H5', 'H6', 'Blockqoute', 'Code'], }, fontFamily: { options: ['Arial', 'Georgia', 'Impact', 'Tahoma', 'Times New Roman', 'Verdana'], className: undefined, component: undefined, dropdownClassName: undefined, }, colorPicker: { className: undefined, component: undefined, popupClassName: undefined, }, }} /> ); };

export default WYSIWYGEditor; `

abhayverma112 avatar May 25 '22 09:05 abhayverma112

Any solution for this so far? I would like to use the Editor as readOnly to display the content coming from my database. This means I need to style the fontsize and family to match my application design. How best can I set a default font family and size and color for the editor content?

kingsleydev19 avatar Jul 11 '22 08:07 kingsleydev19

.DraftEditor-root {
  font-size: 14px;
}

gonenoob avatar Aug 17 '22 08:08 gonenoob

@sparkmediatech Did you ever find a solution by chance? Using the Editor in read only mode as well and would like styles to match my entire application.

Stinny avatar Mar 17 '23 02:03 Stinny

Hi @jpuri , is there already an official solution for setting the default font size that is stored in the editor content and not only in the website CSS? I want to print the editor content with my application, but if the user does not set a custom font size, the default font size is used when printing, which is tiny...

meierac avatar Oct 31 '23 08:10 meierac

the same here

i found this solution:

`import { toggleCustomInlineStyle, getSelectionCustomInlineStyle, } from 'draftjs-utils'; Secondly, on each render(!?) you should execute:

const fontSize = getSelectionCustomInlineStyle(editorState, ['FONTSIZE',]).FONTSIZE if (!fontSize) { setEditorState(toggleCustomInlineStyle(editorState, 'fontSize', 24)) }`

but it's not wor for fontSize and fontFamily on the same time.

jueser avatar Nov 03 '23 10:11 jueser

Adding this line set the font size editorStyle={{ fontSize: 24, }}

<ReactDraftWysiwyg
     wrapperClassName="wrapper-class"
     editorClassName="editor-class"
     toolbarClassName="toolbar-class"
    editorState={editorValue}
    editorStyle={{
      fontSize: 24,
    }}
    }}
  />

maqboolna avatar Feb 01 '24 16:02 maqboolna