From 116a54314db6c681bec4125a9ead067dd4cff505 Mon Sep 17 00:00:00 2001 From: Vitor George Date: Thu, 19 Oct 2023 14:38:30 +0100 Subject: [PATCH] Improve comments, refactor numbering context --- app/assets/scripts/context/numbering.js | 72 ++----------------- .../apply-number-captions-to-document.js | 5 +- 2 files changed, 9 insertions(+), 68 deletions(-) diff --git a/app/assets/scripts/context/numbering.js b/app/assets/scripts/context/numbering.js index eff391e8..1c59c851 100644 --- a/app/assets/scripts/context/numbering.js +++ b/app/assets/scripts/context/numbering.js @@ -1,9 +1,10 @@ import { createContext, useCallback, useMemo, useState } from 'react'; +/** + * Provides a context for equation numbering. + */ export function useNumberingProviderValue() { const [registeredEquations, setRegisteredEquations] = useState({}); - const [registeredImages, setRegisteredImages] = useState({}); - const [registeredTables, setRegisteredTables] = useState({}); const registerEquation = useCallback((key) => { setRegisteredEquations((prevElements) => { @@ -19,34 +20,6 @@ export function useNumberingProviderValue() { }); }, []); - const registerImage = useCallback((key) => { - setRegisteredImages((prevElements) => { - if (prevElements[key]) { - return prevElements; - } - - const numElements = Object.keys(prevElements).length; - return { - ...prevElements, - [key]: numElements + 1 - }; - }); - }, []); - - const registerTable = useCallback((key) => { - setRegisteredTables((prevElements) => { - if (prevElements[key]) { - return prevElements; - } - - const numElements = Object.keys(prevElements).length; - return { - ...prevElements, - [key]: numElements + 1 - }; - }); - }, []); - const getEquationNumbering = useCallback( (key) => { const numbering = registeredEquations[key]; @@ -59,47 +32,12 @@ export function useNumberingProviderValue() { [registeredEquations] ); - const getTableNumbering = useCallback( - (key) => { - const numbering = registeredTables[key]; - if (!numbering) { - return ''; - } - - return `Table ${numbering}: `; - }, - [registeredTables] - ); - - const getImageNumbering = useCallback( - (key) => { - const numbering = registeredImages[key]; - if (!numbering) { - return ''; - } - - return `Figure ${numbering}: `; - }, - [registeredImages] - ); - return useMemo( () => ({ getEquationNumbering, - getTableNumbering, - getImageNumbering, - registerEquation, - registerTable, - registerImage + registerEquation }), - [ - getEquationNumbering, - getTableNumbering, - getImageNumbering, - registerEquation, - registerTable, - registerImage - ] + [getEquationNumbering, registerEquation] ); } diff --git a/app/assets/scripts/utils/apply-number-captions-to-document.js b/app/assets/scripts/utils/apply-number-captions-to-document.js index d142a64f..26cd4eec 100644 --- a/app/assets/scripts/utils/apply-number-captions-to-document.js +++ b/app/assets/scripts/utils/apply-number-captions-to-document.js @@ -5,7 +5,10 @@ import { } from '../components/slate/plugins/constants'; /** - * Include table numbers and move captions before the table in the document. + * Include tables and figures numbers to their captions. We don't use a + * numbering context (like equation numbering) because we also need to change + * the position of the caption in the document, which is not possible with the + * current implementation of equation numbering context. */ export function applyNumberCaptionsToDocument(document) { // Section id list in the order they should appear in the document