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-03-23 17:54:30 +03:00
|
|
|
|
2021-03-30 17:21:54 +03:00
|
|
|
import { generateNumberByStep } from "~/common/utilities";
|
2021-03-26 21:30:02 +03:00
|
|
|
|
2021-03-23 17:54:30 +03:00
|
|
|
export const useFont = ({ url, name }, deps) => {
|
|
|
|
const [loading, setLoading] = React.useState(false);
|
|
|
|
const prevName = React.useRef(name);
|
|
|
|
|
|
|
|
if (!window.$SLATES_LOADED_FONTS) window.$SLATES_LOADED_FONTS = [];
|
|
|
|
const alreadyLoaded = window.$SLATES_LOADED_FONTS.includes(name);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (alreadyLoaded) return;
|
|
|
|
|
|
|
|
setLoading(true);
|
|
|
|
const customFonts = new FontFace(name, `url(${url})`);
|
|
|
|
customFonts.load().then((loadedFont) => {
|
|
|
|
document.fonts.add(loadedFont);
|
2021-03-24 20:55:12 +03:00
|
|
|
prevName.current = name;
|
2021-03-23 17:54:30 +03:00
|
|
|
setLoading(false);
|
2021-03-24 20:55:12 +03:00
|
|
|
|
2021-03-23 17:54:30 +03:00
|
|
|
window.$SLATES_LOADED_FONTS.push(name);
|
|
|
|
});
|
|
|
|
}, deps);
|
2021-03-24 20:55:12 +03:00
|
|
|
|
|
|
|
return { isFontLoading: loading, fontName: alreadyLoaded ? name : prevName.current };
|
2021-03-23 17:54:30 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const initialState = {
|
|
|
|
context: {
|
|
|
|
darkmode: true,
|
|
|
|
showSettings: true,
|
2021-03-30 17:21:54 +03:00
|
|
|
customViewContent: "",
|
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"];
|
|
|
|
const SIZE_OPTIONS = { min: 12, max: 72, step: 2 };
|
|
|
|
const LINE_HEIGHT_OPTIONS = { min: 40, max: 400, step: 20 };
|
|
|
|
const TRACKING_OPTIONS = { min: -1, max: 1.5, step: 0.05 };
|
|
|
|
const COLUMN_OPTIONS = { min: 1, max: 6, step: 1 };
|
|
|
|
|
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) {
|
|
|
|
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":
|
|
|
|
return { ...state, view: action.value };
|
2021-03-30 17:21:54 +03:00
|
|
|
case "UPDATE_CUSTOM_VIEW":
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
view: "custom",
|
|
|
|
customView: action.payload.customView,
|
|
|
|
context: { ...state.context, customViewContent: action.payload.customViewContent },
|
|
|
|
};
|
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,
|
|
|
|
settings: {
|
|
|
|
valign: generateOption(VALIGN_OPTIONS),
|
|
|
|
textAlign: generateOption(ALIGN_OPTIONS),
|
|
|
|
fontSize: generateNumberByStep(SIZE_OPTIONS),
|
|
|
|
column: generateNumberByStep(COLUMN_OPTIONS),
|
|
|
|
lineHeight: generateNumberByStep(LINE_HEIGHT_OPTIONS),
|
|
|
|
tracking: generateNumberByStep(TRACKING_OPTIONS).toPrecision(2),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
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),
|
|
|
|
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-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
|
|
|
};
|