Default selected font issue for custom font in the Typography.
Question
There is one font issue when i click on any element it not show the current font of the element, i added custom code for add my custom fonts. code work perfectly but it not show the selected font by default when i select an paragraph or any text.
editor.on('load', function () {
styleManager = editor.StyleManager;
fontProperty = styleManager.getProperty('Typography', 'font-family');
var list = [];
fontProperty.set('list', list);
list = [
fontProperty.addOption({value: "'Oswald', sans-serif", name: 'Oswald'}),
fontProperty.addOption({value: "Helvetica Neue,Helvetica,Arial,sans-serif", name: 'Helvetica'}),
fontProperty.addOption({value: "sans-serif", name: 'sans-serif'}),
fontProperty.addOption({value: "Times New Roman", name: 'Times New Roman'}),
fontProperty.addOption({value: "Arial Black", name: 'Arial Black'}),
fontProperty.addOption({value: "Tahoma", name: 'Tahoma'}),
fontProperty.addOption({value: "Verdana, Geneva, sans-serif", name: 'Verdana'}),
fontProperty.addOption({value: "Courier New Courier, monospace", name: 'Courier New Courier'}),
fontProperty.addOption({value: "'Lato', sans-serif", name: 'Lato'}),
fontProperty.addOption({value: "'Open Sans', sans-serif", name: 'Open Sans'}),
fontProperty.addOption({value: "'Montserrat', sans-serif", name: 'Montserrat'}),
];
fontProperty.set('list', list);
styleManager.render();
});
Answers (3)
I ended up using something like the following to have Lato as a custom font and set it as my default font:
editor.on('load', function () {
//Add Lato Font & Set as Default
const fontProperty = editor.StyleManager.getProperty('Typography', 'font-family');
fontProperty.addOption({value: `Lato, Helvetica, Arial, sans-serif`, name: 'Lato'});
fontProperty.set('defaults', `Lato, Helvetica, Arial, sans-serif`);
});
this way Lato is checked by default. And of course have the body CSS declare it as your default font as well.
The problem here is the computed font of the selected component. You need to have that computed font inside your list otherwise you won't see it. As already suggested by @jrking4 you can set your default font-family to the body
i found the answer : you have to add your font in single or double quotes otherwise it wont detect the font like this
editor.on('load', function () {
styleManager = editor.StyleManager;
fontProperty = styleManager.getProperty('Typography', 'font-family');
var list = [];
fontProperty.set('list', list);
list = [
fontProperty.addOption({value: '"Open Sans"', name: 'Open Sans'}),
fontProperty.addOption({value: "Montserrat", name: 'Montserrat'}),
fontProperty.addOption({value: "Lato", name: 'Lato'}),
fontProperty.addOption({value: '"Source Sans Pro"', name: 'Source Sans Pro'}),
fontProperty.addOption({value: "Raleway", name: 'Raleway'}),
fontProperty.addOption({value: "Roboto", name: 'Roboto'}),
fontProperty.addOption({value: "Oswald", name: 'Oswald'}),
// fontProperty.addOption({value: '"Helvetica Neue"', name: 'Helvetica'}),
// fontProperty.addOption({value: '"Times New Roman"', name: 'Times New Roman'}),
// fontProperty.addOption({value: '"Arial Black"', name: 'Arial Black'}),
// fontProperty.addOption({value: '"Tahoma"', name: 'Tahoma'}),
// fontProperty.addOption({value: '"Verdana"', name: 'Verdana'}),
// fontProperty.addOption({value: '"Courier New Courier"', name: 'Courier New Courier'}),
];
fontProperty.set('list', list);
styleManager.render();
});
because we write font in our css like : font-family: 'Oswald' html in browse auto convert single quotes to double quotes, when the font match with html and included font in JS it will show in the drop down but some font like Lato is not detect so and this font in browser appear like : font -family : Lato , without single quotes so i wrote this font only double quotes. and other are double quotes inside single quotes i hope someone who has this issue will fix.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2715
I have added the custom font using the method below but that font is only working in few cases
There is one font issue when i click on any element it not show the current font of the element, i added custom code for add my custom font...
Issue #857
[Question]: How do I add a custom font in the Typography | Font dropdown?
Hi, First off, this is what I've been looking for for a long time so thanks for your efforts. Next, I'd like to add a font in the Typograph...
Issue #371
Enable the default RTE with a custom function
Do my API call correctly, want to add a RTE with custom function to add a link/open new windows/open modal: Command work for fontSize but t...
Issue #492
Insert Text at cursor position inside text
Hi, We are evaluating GrapesJS for one of requirement. As part of the evaluation i am creating custom command 'insert placeholder', what it...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.