|
| 1 | +# Grapesjs Fonts |
| 2 | + |
| 3 | +Custom Fonts plugin for grapesjs |
| 4 | + |
| 5 | +> This code is part of a bigger project for me (@lexoyo): [Silex v3](https://github.com/silexlabs/Silex/tree/v3) |
| 6 | +
|
| 7 | +## About this plugin |
| 8 | + |
| 9 | +Links |
| 10 | + |
| 11 | +* [DEMO on Codepen](https://codepen.io/lexoyo/pen/zYLWdxY) |
| 12 | +* [Npm package](https://www.npmjs.com/package/@silexlabs/grapesjs-fonts) |
| 13 | +* [Discussion about this plugins and Symbols, bug report etc](https://github.com/artf/grapesjs/discussions/4317) |
| 14 | +* [Discussion about ongoing developments](https://github.com/artf/grapesjs/discussions/4858#discussioncomment-4756119) |
| 15 | + |
| 16 | +It looks like this: |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +The plugin currently has these features |
| 23 | + |
| 24 | +* API to add / remove fonts from the site (from goole font name) |
| 25 | +* Updates the DOM and the "font family" dropdown |
| 26 | +* Save the fonts with the site data |
| 27 | +* Load the fonts when site data is loaded (add to the DOM on load) |
| 28 | +* UI to manage fonts |
| 29 | +* Integration with google API |
| 30 | +* Store google fonts list in local storage for performance and API quotas |
| 31 | + |
| 32 | +Limitations: |
| 33 | + |
| 34 | +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. |
| 35 | + |
| 36 | +I would love help on this: |
| 37 | + |
| 38 | +* Code review and suggestions |
| 39 | +* Support Google fonts V3 API |
| 40 | +* Other providers than Google fonts |
| 41 | + |
| 42 | +See the "Development" section bellow to contribute |
| 43 | + |
| 44 | +### Motivations |
| 45 | + |
| 46 | +I saw discussions and issues like "How can i add custom fonts in grapesjs editor? #4563" |
| 47 | + |
| 48 | +What seems to work for me is |
| 49 | + |
| 50 | +1. update the "font family" dropdown |
| 51 | + ``` |
| 52 | + const styleManager = editor.StyleManager |
| 53 | + const fontProperty = styleManager.getProperty('typography', 'font-family') |
| 54 | + fontProperty.setOptions(fonts) |
| 55 | + styleManager.render() |
| 56 | + ``` |
| 57 | +1. update the DOM to display the font correctly: add style elements to the editor.Canvas.getDocument() |
| 58 | +
|
| 59 | +This is quite easy but here are the things which took me time as I implemented google fonts |
| 60 | +
|
| 61 | +* use google fonts api to select fonts and get their name, variants, weights |
| 62 | +* build the URL of the fonts to load |
| 63 | +* the UI to manage and install fonts |
| 64 | +
|
| 65 | +## Use the plugin in your website builder |
| 66 | +
|
| 67 | +### HTML |
| 68 | +```html |
| 69 | +
|
| 70 | +<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet"> |
| 71 | +<script src="https://unpkg.com/grapesjs"></script> |
| 72 | +<script src="https://unpkg.com/grapesjs-fonts"></script> |
| 73 | +
|
| 74 | +<div id="gjs"></div> |
| 75 | +``` |
| 76 | + |
| 77 | +### JS |
| 78 | +```js |
| 79 | +const editor = grapesjs.init({ |
| 80 | + container: '#gjs', |
| 81 | + height: '100%', |
| 82 | + fromElement: true, |
| 83 | + storageManager: false, |
| 84 | + plugins: ['@silexlabs/grapesjs-fonts'], |
| 85 | +}); |
| 86 | +``` |
| 87 | + |
| 88 | +This will make sure the fonts are saved and loaded with the website data |
| 89 | + |
| 90 | +Here is how to open the fonts dialog: |
| 91 | + |
| 92 | +```js |
| 93 | +editor.runCommand('open-fonts') |
| 94 | +``` |
| 95 | + |
| 96 | +And you can use the plugin's API: |
| 97 | + |
| 98 | +```js |
| 99 | +// TODO: expose the API |
| 100 | + |
| 101 | +``` |
| 102 | + |
| 103 | +### CSS |
| 104 | +```css |
| 105 | +body, html { |
| 106 | + margin: 0; |
| 107 | + height: 100%; |
| 108 | +} |
| 109 | +``` |
| 110 | + |
| 111 | +Also you should style the dialog: |
| 112 | + |
| 113 | +```css |
| 114 | +.silex-form select { |
| 115 | + ... |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | + |
| 120 | +## Summary |
| 121 | + |
| 122 | +* Plugin name: `@silexlabs/grapesjs-fonts` |
| 123 | +* Components |
| 124 | + * `component-id-1` |
| 125 | + * `component-id-2` |
| 126 | + * ... |
| 127 | +* Blocks |
| 128 | + * `block-id-1` |
| 129 | + * `block-id-2` |
| 130 | + * ... |
| 131 | + |
| 132 | + |
| 133 | + |
| 134 | +## Options |
| 135 | + |
| 136 | +| Option | Description | Default | |
| 137 | +|-|-|- |
| 138 | +| `api_key` | Google fonts API key, [see this doc to get an API key](https://developers.google.com/fonts/docs/developer_api#APIKey) | `default value` | |
| 139 | + |
| 140 | + |
| 141 | + |
| 142 | +## Download |
| 143 | + |
| 144 | +* CDN |
| 145 | + * `https://unpkg.com/@silexlabs/grapesjs-fonts` |
| 146 | +* NPM |
| 147 | + * `npm i @silexlabs/grapesjs-fonts` |
| 148 | +* GIT |
| 149 | + * `git clone https://github.com/silexlabs/grapesjs-fonts.git` |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | +## Usage |
| 154 | + |
| 155 | +Directly in the browser |
| 156 | +```html |
| 157 | +<link href="https://unpkg.com/grapesjs/dist/css/grapes.min.css" rel="stylesheet"/> |
| 158 | +<script src="https://unpkg.com/grapesjs"></script> |
| 159 | +<script src="path/to/grapesjs-fonts.min.js"></script> |
| 160 | + |
| 161 | +<div id="gjs"></div> |
| 162 | + |
| 163 | +<script type="text/javascript"> |
| 164 | + var editor = grapesjs.init({ |
| 165 | + container: '#gjs', |
| 166 | + // ... |
| 167 | + plugins: ['@silexlabs/grapesjs-fonts'], |
| 168 | + pluginsOpts: { |
| 169 | + '@silexlabs/grapesjs-fonts': { |
| 170 | + api_key: '...', |
| 171 | + } |
| 172 | + } |
| 173 | + }); |
| 174 | +</script> |
| 175 | +``` |
| 176 | + |
| 177 | +Modern javascript |
| 178 | +```js |
| 179 | +import grapesjs from 'grapesjs'; |
| 180 | +import plugin from '@silexlabs/grapesjs-fonts'; |
| 181 | +import 'grapesjs/dist/css/grapes.min.css'; |
| 182 | + |
| 183 | +const editor = grapesjs.init({ |
| 184 | + container : '#gjs', |
| 185 | + // ... |
| 186 | + plugins: [plugin], |
| 187 | + pluginsOpts: { |
| 188 | + [plugin]: { |
| 189 | + api_key: '...', |
| 190 | + } |
| 191 | + } |
| 192 | + // or |
| 193 | + plugins: [ |
| 194 | + editor => plugin(editor, { |
| 195 | + api_key: '...', |
| 196 | + }), |
| 197 | + ], |
| 198 | +}); |
| 199 | +``` |
| 200 | + |
| 201 | +## Development |
| 202 | + |
| 203 | +Clone the repository |
| 204 | + |
| 205 | +```sh |
| 206 | +$ git clone https://github.com/silexlabs/grapesjs-fonts.git |
| 207 | +$ cd grapesjs-fonts |
| 208 | +``` |
| 209 | + |
| 210 | +Install dependencies |
| 211 | + |
| 212 | +```sh |
| 213 | +$ npm i |
| 214 | +``` |
| 215 | + |
| 216 | +Start the dev server |
| 217 | + |
| 218 | +```sh |
| 219 | +$ npm start |
| 220 | +``` |
| 221 | + |
| 222 | +Build the source |
| 223 | + |
| 224 | +```sh |
| 225 | +$ npm run build |
| 226 | +``` |
| 227 | + |
| 228 | +## License |
| 229 | + |
| 230 | +MIT |
| 231 | + |
0 commit comments