Skip to content

Commit 3ecdb69

Browse files
committed
refactor(texteditor): general code improvments
1 parent ab59c6f commit 3ecdb69

File tree

3 files changed

+30
-47
lines changed

3 files changed

+30
-47
lines changed

contentcuration/contentcuration/frontend/shared/views/TipTapEditor/TipTapEditor/TipTapEditorStrings.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,10 @@ const MESSAGES = {
291291
message: 'Special Characters',
292292
context: 'Title for the menu containing special characters and mathematical symbols.',
293293
},
294+
loadingFormulas: {
295+
message: 'Loading math editor',
296+
context: 'Text displayed while the math editor is being loaded.',
297+
},
294298

295299
// Error Messages
296300
noFileProvided: {

contentcuration/contentcuration/frontend/shared/views/TipTapEditor/TipTapEditor/components/math/FormulasMenu.vue

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
v-if="!mathLiveLoaded"
3434
class="loading-placeholder"
3535
>
36-
Loading math editor...
36+
{{ loadingFormulas$() }}...
3737
</div>
3838
<math-field
3939
v-else
@@ -107,7 +107,7 @@
107107

108108
<script>
109109
110-
import { ref, onMounted, defineComponent, computed } from 'vue';
110+
import { ref, onMounted, defineComponent, computed, nextTick } from 'vue';
111111
import { useFocusTrap } from '../../composables/useFocusTrap';
112112
import { getTipTapEditorStrings } from '../../TipTapEditorStrings';
113113
import { useMathLiveLocale } from '../../composables/useMathLiveLocale';
@@ -131,8 +131,6 @@
131131
// Dynamic import of mathlive
132132
await import(/* webpackChunkName: "mathlive" */ 'mathlive');
133133
134-
mathLiveLoaded.value = true;
135-
136134
// Configure MathLive after it's loaded
137135
if (typeof window !== 'undefined' && window.MathfieldElement) {
138136
window.MathfieldElement.soundsDirectory = null;
@@ -142,6 +140,8 @@
142140
}
143141
}
144142
143+
mathLiveLoaded.value = true;
144+
145145
// Initialize the mathfield after MathLive is loaded
146146
await initializeMathfield();
147147
} catch (error) {
@@ -152,7 +152,7 @@
152152
153153
const initializeMathfield = async () => {
154154
// Wait for next tick to ensure the mathfield element is rendered
155-
await new Promise(resolve => setTimeout(resolve, 0));
155+
await nextTick();
156156
157157
if (mathfieldEl.value) {
158158
mathfieldEl.value.mathVirtualKeyboardPolicy = 'manual';
@@ -226,7 +226,8 @@
226226
}
227227
};
228228
229-
const { formulasMenuTitle$, insert$, closeModal$ } = getTipTapEditorStrings();
229+
const { formulasMenuTitle$, insert$, closeModal$, loadingFormulas$ } =
230+
getTipTapEditorStrings();
230231
const formulasStrings = getFormulasStrings();
231232
232233
// Transform symbols data to use translations
@@ -265,6 +266,7 @@
265266
formulasMenuTitle$,
266267
insert$,
267268
closeModal$,
269+
loadingFormulas$,
268270
isFormulaEmpty,
269271
};
270272
},

contentcuration/contentcuration/frontend/shared/views/TipTapEditor/TipTapEditor/composables/useModalPositioning.js

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ref, watch } from 'vue';
2+
import { throttle } from 'lodash';
23
import { useBreakpoint } from './useBreakpoint';
34

45
export function useModalPositioning() {
@@ -8,9 +9,6 @@ export function useModalPositioning() {
89
const anchorElement = ref(null);
910
const { isMobile } = useBreakpoint();
1011

11-
let scrollRaf = null;
12-
let resizeRaf = null;
13-
1412
const updatePosition = () => {
1513
if (!anchorElement.value || isModalCentered.value || isMobile.value) {
1614
return;
@@ -24,29 +22,20 @@ export function useModalPositioning() {
2422
};
2523
};
2624

27-
// Throttle scroll and resize events
28-
const throttledUpdatePosition = () => {
29-
if (scrollRaf) {
30-
cancelAnimationFrame(scrollRaf);
25+
const handleResize = () => {
26+
if (isModalOpen.value) {
27+
// Re-evaluate positioning on resize
28+
if (isMobile.value && !isModalCentered.value) {
29+
setCenteredPosition();
30+
} else if (!isMobile.value && anchorElement.value) {
31+
updatePosition();
32+
}
3133
}
32-
scrollRaf = requestAnimationFrame(updatePosition);
3334
};
3435

35-
const throttledHandleResize = () => {
36-
if (resizeRaf) {
37-
cancelAnimationFrame(resizeRaf);
38-
}
39-
resizeRaf = requestAnimationFrame(() => {
40-
if (isModalOpen.value) {
41-
// Re-evaluate positioning on resize
42-
if (isMobile.value && !isModalCentered.value) {
43-
setCenteredPosition();
44-
} else if (!isMobile.value && anchorElement.value) {
45-
updatePosition();
46-
}
47-
}
48-
});
49-
};
36+
// Throttle scroll and resize events
37+
const throttledUpdatePosition = throttle(updatePosition, 10);
38+
const throttledHandleResize = throttle(handleResize, 10);
5039

5140
const setCenteredPosition = () => {
5241
isModalCentered.value = true;
@@ -102,31 +91,19 @@ export function useModalPositioning() {
10291
document.removeEventListener('mousedown', clickOutsideHandler, true);
10392
window.removeEventListener('scroll', throttledUpdatePosition, true);
10493
window.removeEventListener('resize', throttledHandleResize, true);
105-
// Cancel any pending animation frames
106-
if (scrollRaf) {
107-
cancelAnimationFrame(scrollRaf);
108-
scrollRaf = null;
109-
}
110-
if (resizeRaf) {
111-
cancelAnimationFrame(resizeRaf);
112-
resizeRaf = null;
113-
}
94+
// Cancel any pending throttled calls
95+
throttledUpdatePosition.cancel();
96+
throttledHandleResize.cancel();
11497
}
11598
});
11699
};
117100

118101
const cleanup = () => {
119102
window.removeEventListener('scroll', throttledUpdatePosition, true);
120103
window.removeEventListener('resize', throttledHandleResize, true);
121-
// Cancel any pending animation frames
122-
if (scrollRaf) {
123-
cancelAnimationFrame(scrollRaf);
124-
scrollRaf = null;
125-
}
126-
if (resizeRaf) {
127-
cancelAnimationFrame(resizeRaf);
128-
resizeRaf = null;
129-
}
104+
// Cancel any pending throttled calls
105+
throttledUpdatePosition.cancel();
106+
throttledHandleResize.cancel();
130107
};
131108

132109
return {

0 commit comments

Comments
 (0)