2021-03-23 17:54:30 +03:00
|
|
|
import * as React from "react";
|
2021-03-24 20:55:12 +03:00
|
|
|
import * as Events from "~/common/custom-events";
|
2021-06-09 01:53:30 +03:00
|
|
|
import * as Content from "~/components/core/FontFrame/Views/content";
|
2021-03-07 23:53:54 +03:00
|
|
|
import * as Strings from "~/common/strings";
|
2021-03-23 17:54:30 +03:00
|
|
|
|
2021-03-30 17:21:54 +03:00
|
|
|
import { generateNumberByStep } from "~/common/utilities";
|
2021-08-05 22:45:52 +03:00
|
|
|
import { useIsomorphicLayoutEffect } from "~/common/hooks";
|
2021-03-26 21:30:02 +03:00
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
export const useFont = ({ cid }, deps) => {
|
|
|
|
const url = Strings.getURLfromCID(cid);
|
2021-04-08 22:00:09 +03:00
|
|
|
const [fetchState, setFetchState] = React.useState({ loading: false, error: null });
|
2021-03-07 23:53:54 +03:00
|
|
|
const prevName = React.useRef(cid);
|
2021-03-23 17:54:30 +03:00
|
|
|
|
2021-05-27 11:20:34 +03:00
|
|
|
if (typeof window !== "undefined" && !window.$SLATES_LOADED_FONTS) {
|
|
|
|
window.$SLATES_LOADED_FONTS = [];
|
|
|
|
}
|
|
|
|
const alreadyLoaded =
|
|
|
|
(typeof window !== "undefined" && window.$SLATES_LOADED_FONTS.includes(cid)) || false;
|
2021-03-23 17:54:30 +03:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
2021-05-27 11:20:34 +03:00
|
|
|
if (!window) return;
|
|
|
|
|
2021-04-08 22:00:09 +03:00
|
|
|
if (alreadyLoaded) {
|
|
|
|
setFetchState((prev) => ({ ...prev, error: null }));
|
|
|
|
return;
|
|
|
|
}
|
2021-03-23 17:54:30 +03:00
|
|
|
|
2021-04-08 22:00:09 +03:00
|
|
|
setFetchState((prev) => ({ ...prev, error: null, loading: true }));
|
2021-03-07 23:53:54 +03:00
|
|
|
const customFonts = new FontFace(cid, `url(${url})`);
|
2021-04-08 22:00:09 +03:00
|
|
|
customFonts
|
|
|
|
.load()
|
|
|
|
.then((loadedFont) => {
|
|
|
|
document.fonts.add(loadedFont);
|
2021-03-07 23:53:54 +03:00
|
|
|
prevName.current = cid;
|
2021-03-24 20:55:12 +03:00
|
|
|
|
2021-04-08 22:00:09 +03:00
|
|
|
setFetchState((prev) => ({ ...prev, loading: false }));
|
2021-03-07 23:53:54 +03:00
|
|
|
window.$SLATES_LOADED_FONTS.push(cid);
|
2021-04-08 22:00:09 +03:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
setFetchState({ loading: false, error: err });
|
|
|
|
});
|
2021-03-23 17:54:30 +03:00
|
|
|
}, deps);
|
2021-03-24 20:55:12 +03:00
|
|
|
|
2021-03-30 21:52:43 +03:00
|
|
|
return {
|
2021-04-08 22:00:09 +03:00
|
|
|
isFontLoading: fetchState.loading,
|
|
|
|
error: fetchState.error,
|
2021-03-30 21:52:43 +03:00
|
|
|
// NOTE(Amine): show previous font while we load the new one.
|
2021-03-07 23:53:54 +03:00
|
|
|
fontName: alreadyLoaded ? cid : prevName.current,
|
2021-03-30 21:52:43 +03:00
|
|
|
};
|
2021-03-23 17:54:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
context: {
|
|
|
|
darkmode: true,
|
|
|
|
showSettings: true,
|
2021-03-31 22:00:12 +03:00
|
|
|
sentence: Content.sentences[0],
|
2021-03-31 22:57:27 +03:00
|
|
|
paragraph: Content.paragraphs[0],
|
|
|
|
customViewContent: Content.sentences[1],
|
2021-03-23 17:54:30 +03:00
|
|
|
settings: {
|
|
|
|
valign: "center",
|
|
|
|
textAlign: "left",
|
|
|
|
fontSize: 24,
|
2021-03-25 20:34:55 +03:00
|
|
|
column: 2,
|
2021-03-24 20:55:12 +03:00
|
|
|
lineHeight: 100,
|
2021-03-23 17:54:30 +03:00
|
|
|
tracking: 0,
|
|
|
|
},
|
|
|
|
},
|
2021-03-24 22:04:51 +03:00
|
|
|
view: "sentence",
|
2021-03-30 17:21:54 +03:00
|
|
|
customView: "sentence",
|
2021-03-23 17:54:30 +03:00
|
|
|
};
|
|
|
|
|
2021-03-26 21:30:02 +03:00
|
|
|
const VIEW_OPTIONS = ["custom", "glyphs", "sentence", "paragraph"];
|
|
|
|
const VALIGN_OPTIONS = ["top", "center", "bottom"];
|
|
|
|
const ALIGN_OPTIONS = ["left", "center", "right"];
|
2021-03-31 22:00:12 +03:00
|
|
|
const SIZE_OPTIONS = { min: 8, max: 320, step: 1 };
|
|
|
|
const LINE_HEIGHT_OPTIONS = { min: 50, max: 300, step: 10 };
|
|
|
|
const TRACKING_OPTIONS = { min: -0.1, max: 0.2, step: 0.01 };
|
|
|
|
const COLUMN_OPTIONS = { min: 1, max: 4, step: 1 };
|
2021-03-26 21:30:02 +03:00
|
|
|
|
2021-03-23 17:54:30 +03:00
|
|
|
const reducer = (state, action) => {
|
2021-03-24 20:55:12 +03:00
|
|
|
const updateSettingsField = (field, newValue) => ({
|
|
|
|
...state,
|
|
|
|
context: {
|
|
|
|
...state.context,
|
|
|
|
settings: { ...state.context.settings, [field]: newValue },
|
|
|
|
},
|
|
|
|
});
|
2021-03-23 17:54:30 +03:00
|
|
|
switch (action.type) {
|
2021-03-30 21:52:43 +03:00
|
|
|
case "INITIATE_STATE": {
|
2021-03-31 22:00:12 +03:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...action.value,
|
|
|
|
context: {
|
|
|
|
...state.context,
|
|
|
|
...action.value.context,
|
|
|
|
settings: { ...state.context.settings, ...action.value.context.settings },
|
|
|
|
},
|
|
|
|
};
|
2021-03-30 21:52:43 +03:00
|
|
|
}
|
2021-03-23 17:54:30 +03:00
|
|
|
case "SET_DARK_MODE":
|
|
|
|
return { ...state, context: { ...state.context, darkmode: true } };
|
|
|
|
case "SET_LIGHT_MODE":
|
|
|
|
return { ...state, context: { ...state.context, darkmode: false } };
|
|
|
|
case "TOGGLE_SETTINGS_VISIBILITY":
|
|
|
|
return { ...state, context: { ...state.context, showSettings: !state.context.showSettings } };
|
2021-03-24 20:55:12 +03:00
|
|
|
case "UPDATE_FONT_SIZE":
|
|
|
|
return updateSettingsField("fontSize", action.value);
|
|
|
|
case "UPDATE_LINE_HEIGHT":
|
|
|
|
return updateSettingsField("lineHeight", action.value);
|
|
|
|
case "UPDATE_TRACKING":
|
|
|
|
return updateSettingsField("tracking", action.value);
|
|
|
|
case "UPDATE_COLUMN":
|
|
|
|
return updateSettingsField("column", action.value);
|
|
|
|
case "UPDATE_TEXT_ALIGN":
|
|
|
|
return updateSettingsField("textAlign", action.value);
|
|
|
|
case "UPDATE_VERTICAL_ALIGN":
|
|
|
|
return updateSettingsField("valign", action.value);
|
2021-03-24 22:04:51 +03:00
|
|
|
case "UPDATE_VIEW":
|
2021-03-30 21:52:43 +03:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
view: action.value,
|
2021-04-01 17:05:51 +03:00
|
|
|
context: { ...state.context },
|
2021-03-30 21:52:43 +03:00
|
|
|
};
|
2021-03-30 17:21:54 +03:00
|
|
|
case "UPDATE_CUSTOM_VIEW":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
view: "custom",
|
|
|
|
customView: action.payload.customView,
|
2021-03-30 21:52:43 +03:00
|
|
|
context: {
|
|
|
|
...state.context,
|
|
|
|
customViewContent: action.payload.customViewContent,
|
|
|
|
},
|
2021-03-30 17:21:54 +03:00
|
|
|
};
|
2021-03-26 21:30:02 +03:00
|
|
|
case "RESET":
|
|
|
|
return { ...initialState };
|
|
|
|
case "FEELING_LUCKY":
|
|
|
|
const generateOption = (options) =>
|
|
|
|
options[generateNumberByStep({ min: 0, max: options.length - 1, step: 1 })];
|
|
|
|
const generatedView = generateOption(VIEW_OPTIONS.slice(1));
|
|
|
|
if (generatedView === "glyphs") {
|
|
|
|
return { ...state, view: generatedView };
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
view: generatedView,
|
|
|
|
context: {
|
|
|
|
...state.context,
|
2021-03-31 22:00:12 +03:00
|
|
|
sentence: generateOption(Content.sentences),
|
|
|
|
paragraph: generateOption(Content.paragraphs),
|
2021-03-26 21:30:02 +03:00
|
|
|
settings: {
|
|
|
|
valign: generateOption(VALIGN_OPTIONS),
|
|
|
|
textAlign: generateOption(ALIGN_OPTIONS),
|
2021-03-31 22:00:12 +03:00
|
|
|
fontSize: generateNumberByStep({ min: 16, max: 72, step: 1 }),
|
|
|
|
column: generateNumberByStep({ min: 1, max: 4, step: 1 }),
|
|
|
|
lineHeight: generateNumberByStep({ min: 100, max: 200, step: 10 }),
|
|
|
|
tracking: generateNumberByStep({ min: 0, max: 0.1, step: 0.01 }).toPrecision(1),
|
2021-03-26 21:30:02 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2021-03-23 17:54:30 +03:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export const useFontControls = () => {
|
|
|
|
const [current, send] = React.useReducer(reducer, initialState);
|
2021-03-24 20:55:12 +03:00
|
|
|
const handleDarkMode = (state) =>
|
|
|
|
Events.dispatchCustomEvent({
|
|
|
|
name: "set-slate-theme",
|
|
|
|
detail: { fontPreviewDarkMode: state },
|
|
|
|
});
|
2021-03-24 22:04:51 +03:00
|
|
|
const handlers = React.useMemo(
|
|
|
|
() => ({
|
|
|
|
setDarkMode: () => handleDarkMode(true),
|
|
|
|
setLightMode: () => handleDarkMode(false),
|
2021-03-30 21:52:43 +03:00
|
|
|
initialState: (state) => send({ type: "INITIATE_STATE", value: state }),
|
2021-03-24 22:04:51 +03:00
|
|
|
toggleSettings: () => send({ type: "TOGGLE_SETTINGS_VISIBILITY" }),
|
|
|
|
updateFontSize: (v) => send({ type: "UPDATE_FONT_SIZE", value: v }),
|
|
|
|
updateLineHeight: (v) => send({ type: "UPDATE_LINE_HEIGHT", value: v }),
|
|
|
|
updateTracking: (v) => send({ type: "UPDATE_TRACKING", value: v }),
|
|
|
|
updateColumn: (v) => send({ type: "UPDATE_COLUMN", value: v }),
|
|
|
|
updateTextAlign: (v) => send({ type: "UPDATE_TEXT_ALIGN", value: v }),
|
|
|
|
updateVerticalAlign: (v) => send({ type: "UPDATE_VERTICAL_ALIGN", value: v }),
|
|
|
|
updateView: (v) => send({ type: "UPDATE_VIEW", value: v }),
|
2021-03-30 17:21:54 +03:00
|
|
|
updateCustomView: ({ customView, customViewContent }) =>
|
|
|
|
send({ type: "UPDATE_CUSTOM_VIEW", payload: { customView, customViewContent } }),
|
2021-03-26 21:30:02 +03:00
|
|
|
resetLayout: () => send({ type: "RESET" }),
|
|
|
|
getRandomLayout: () => send({ type: "FEELING_LUCKY" }),
|
2021-03-24 22:04:51 +03:00
|
|
|
}),
|
|
|
|
[]
|
|
|
|
);
|
2021-03-30 21:52:43 +03:00
|
|
|
|
|
|
|
const FONT_PREVIEW_STORAGE_TOKEN = "SLATE_FONT_PREVIEW_SETTINGS";
|
|
|
|
|
2021-08-05 22:45:52 +03:00
|
|
|
useIsomorphicLayoutEffect(() => {
|
2021-03-30 21:52:43 +03:00
|
|
|
const initialState = JSON.parse(localStorage.getItem(FONT_PREVIEW_STORAGE_TOKEN));
|
|
|
|
if (initialState) handlers.initialState(initialState);
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
localStorage.setItem(FONT_PREVIEW_STORAGE_TOKEN, JSON.stringify(current));
|
|
|
|
}, [current]);
|
|
|
|
|
2021-03-26 21:30:02 +03:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
...current,
|
|
|
|
defaultOptions: {
|
|
|
|
VIEW_OPTIONS,
|
|
|
|
VALIGN_OPTIONS,
|
|
|
|
ALIGN_OPTIONS,
|
|
|
|
SIZE_OPTIONS,
|
|
|
|
LINE_HEIGHT_OPTIONS,
|
|
|
|
TRACKING_OPTIONS,
|
|
|
|
COLUMN_OPTIONS,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
handlers,
|
|
|
|
];
|
2021-03-23 17:54:30 +03:00
|
|
|
};
|