npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@silexlabs/grapesjs-fonts

v1.0.18

Published

GrapesJS Fonts

Downloads

1,671

Readme

Grapesjs Fonts

Custom Fonts plugin for grapesjs

This code is part of a bigger project: about Silex v3

About this plugin

Links

It looks like this:

Screenshot from 2023-01-20 16-20-56 Screenshot from 2023-01-20 16-19-41

The plugin currently has these features

  • [x] API to add / remove fonts from the site (from goole font name)
  • [x] Updates the DOM and the "font family" dropdown
  • [x] Save the fonts with the site data
  • [x] Load the fonts when site data is loaded (add to the DOM on load)
  • [x] UI to manage fonts
  • [x] Integration with google API
  • [x] Store google fonts list in local storage for performance and API quotas
  • [x] Generate HTML imports for the final website
  • [x] Support Google fonts proxies, e.g fontlay and a privacy-friendly drop-in replacement for Google Fonts, e.g coollabsio's fonts server
  • [ ] Generate CSS imports for the final website
  • [ ] Handle variants and weights
  • [ ] Google fonts V3 API

Limitations

For now this plugin supports only Goolge fonts and use the V2 API. It should be upgraded to V3 and take advantage of variable fonts.

Export / publication

In your app you probaly publish / export the website to a final format (html, css, js, images, etc).

You need to add the font to the final website when you publish/export it, this is not done by the plugin. You can use the provided commands get-fonts-css OR get-fonts-html to get the code to add to the final website.

// get the CSS to add to the final website
const css = editor.runCommand('get-fonts-css')
// Here css is like @import url('https://fonts.googleapis.com/css2?family=Protest+Strike&display=swap')

Or ues the get-fonts-import-html command to get the HTML to add to the final website

// get the HTML to add to the final website
const html = editor.runCommand('get-fonts-html')
// Here html is like <link href="https://fonts.googleapis.com/css2?family=Protest+Strike&display=swap" rel="stylesheet">

You can check how it is done in Silex website builder

Motivations

I saw discussions and issues like "How can i add custom fonts in grapesjs editor? #4563"

What seems to work for me is

  1. update the "font family" dropdown
    const styleManager = editor.StyleManager
    const fontProperty = styleManager.getProperty('typography', 'font-family')
    fontProperty.setOptions(fonts)
    styleManager.render()
  2. update the DOM to display the font correctly: add style elements to the editor.Canvas.getDocument()

This is quite easy but here are the things which took me time as I implemented google fonts

  • use google fonts api to select fonts and get their name, variants, weights
  • build the URL of the fonts to load
  • the UI to manage and install fonts

Use the plugin in your website builder

HTML

<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet">
<script src="https://unpkg.com/grapesjs"></script>
<script src="https://unpkg.com/@silexlabs/grapesjs-fonts"></script>

<div id="gjs"></div>

JS

const editor = grapesjs.init({
  container: '#gjs',
  height: '100%',
  fromElement: true,
  storageManager: false,
  plugins: ['@silexlabs/grapesjs-fonts'],
});

This will make sure the fonts are saved and loaded with the website data

Here is how to open the fonts dialog:

editor.runCommand('open-fonts')

CSS

body, html {
  margin: 0;
  height: 100%;
}

Also you should style the dialog:

.silex-form select {
  ...
}

Options

| Option | Description | Default | |-|-|- | api_key | Google fonts API key, see this doc to get an API key | Required | | server_url | Google fonts server or a free privacy-friendly drop-in replacement for Google Fonts or a proxy server to speed up the load and protect privacy | https://www.googleapis.com |

Download

  • CDN
    • https://unpkg.com/@silexlabs/grapesjs-fonts
  • NPM
    • npm i @silexlabs/grapesjs-fonts
  • GIT
    • git clone https://github.com/silexlabs/grapesjs-fonts.git

Usage

Directly in the browser

<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet"/>
<script src="https://unpkg.com/grapesjs"></script>
<script src="path/to/grapesjs-fonts.min.js"></script>

<div id="gjs"></div>

<script type="text/javascript">
  var editor = grapesjs.init({
      container: '#gjs',
      // ...
      plugins: ['@silexlabs/grapesjs-fonts'],
      pluginsOpts: {
        '@silexlabs/grapesjs-fonts': {
          api_key: '...',
        }
      }
  });
</script>

Modern javascript

import grapesjs from 'grapesjs';
import plugin from '@silexlabs/grapesjs-fonts';
import 'grapesjs/dist/css/grapes.min.css';

const editor = grapesjs.init({
  container : '#gjs',
  // ...
  plugins: [plugin],
  pluginsOpts: {
    [plugin]: {
      api_key: '...',
    }
  }
  // or
  plugins: [
    editor => plugin(editor, {
      api_key: '...',
    }),
  ],
});

Development

Clone the repository

$ git clone https://github.com/silexlabs/grapesjs-fonts.git
$ cd grapesjs-fonts

Install dependencies

$ npm i

Start the dev server

$ npm start

Build the source

$ npm run build

License

MIT