Skip to content

Commit

Permalink
Improve comments, refactor numbering context
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeorge committed Oct 19, 2023
1 parent aa09225 commit 116a543
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 68 deletions.
72 changes: 5 additions & 67 deletions app/assets/scripts/context/numbering.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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];
Expand All @@ -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]
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 116a543

Please sign in to comment.