actions.js
1 month ago
booking.js
1 month ago
cabinet.js
1 month ago
cart.js
1 month ago
catalog.js
1 month ago
coupon.js
1 month ago
customFields.js
1 month ago
customFontFace.js
1 month ago
eventFormCssVars.js
1 month ago
events.js
1 month ago
facebookPixel.js
1 month ago
ivy.js
1 month ago
package.js
1 month ago
panel.js
1 month ago
public.js
1 month ago
renderActions.js
1 month ago
restore.js
1 month ago
slots.js
1 month ago
translation.js
1 month ago
user.js
1 month ago
customFontFace.js
33 lines
| 1 | const CUSTOM_FONT_FACE_STYLE_ID = 'amCustomFont' |
| 2 | |
| 3 | /** |
| 4 | * Injects a single @font-face rule into document head when Amelia uses an uploaded custom font. |
| 5 | * Removes any previous node with the same id (legacy behavior). |
| 6 | * |
| 7 | * @param {{ customFontSelected?: boolean, fontFamily?: string, fontUrl?: string }|null|undefined} fonts |
| 8 | */ |
| 9 | export function applyCustomFontFaceIfSelected(fonts) { |
| 10 | if (!fonts || !fonts.customFontSelected) { |
| 11 | return |
| 12 | } |
| 13 | |
| 14 | if (!fonts.fontFamily || !fonts.fontUrl) { |
| 15 | return |
| 16 | } |
| 17 | |
| 18 | const head = document.head || document.getElementsByTagName('head')[0] |
| 19 | const existing = head.querySelector(`#${CUSTOM_FONT_FACE_STYLE_ID}`) |
| 20 | if (existing) { |
| 21 | existing.remove() |
| 22 | } |
| 23 | |
| 24 | const safeFontFamily = String(fonts.fontFamily).replace(/['\\\n\r]/g, '\\$&') |
| 25 | const safeFontUrl = String(fonts.fontUrl).replace(/["\\)\n\r]/g, '\\$&') |
| 26 | const css = `@font-face { font-family: '${safeFontFamily}'; src: url("${safeFontUrl}"); }` |
| 27 | const style = document.createElement('style') |
| 28 | head.appendChild(style) |
| 29 | style.setAttribute('type', 'text/css') |
| 30 | style.setAttribute('id', CUSTOM_FONT_FACE_STYLE_ID) |
| 31 | style.appendChild(document.createTextNode(css)) |
| 32 | } |
| 33 |