comparison .cms/lib/codemirror/src/edit/global_events.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 import { onBlur } from "../display/focus.js"
2 import { on } from "../util/event.js"
3
4 // These must be handled carefully, because naively registering a
5 // handler for each editor will cause the editors to never be
6 // garbage collected.
7
8 function forEachCodeMirror(f) {
9 if (!document.getElementsByClassName) return
10 let byClass = document.getElementsByClassName("CodeMirror"), editors = []
11 for (let i = 0; i < byClass.length; i++) {
12 let cm = byClass[i].CodeMirror
13 if (cm) editors.push(cm)
14 }
15 if (editors.length) editors[0].operation(() => {
16 for (let i = 0; i < editors.length; i++) f(editors[i])
17 })
18 }
19
20 let globalsRegistered = false
21 export function ensureGlobalHandlers() {
22 if (globalsRegistered) return
23 registerGlobalHandlers()
24 globalsRegistered = true
25 }
26 function registerGlobalHandlers() {
27 // When the window resizes, we need to refresh active editors.
28 let resizeTimer
29 on(window, "resize", () => {
30 if (resizeTimer == null) resizeTimer = setTimeout(() => {
31 resizeTimer = null
32 forEachCodeMirror(onResize)
33 }, 100)
34 })
35 // When the window loses focus, we want to show the editor as blurred
36 on(window, "blur", () => forEachCodeMirror(onBlur))
37 }
38 // Called when the window resizes
39 function onResize(cm) {
40 let d = cm.display
41 // Might be a text scaling operation, clear size caches.
42 d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null
43 d.scrollbarsClipped = false
44 cm.setSize()
45 }