openrat-cms

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

global_events.min.js (1413B)


      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")
     11   for (let i = 0; i < byClass.length; i++) {
     12     let cm = byClass[i].CodeMirror
     13     if (cm) f(cm)
     14   }
     15 }
     16 
     17 let globalsRegistered = false
     18 export function ensureGlobalHandlers() {
     19   if (globalsRegistered) return
     20   registerGlobalHandlers()
     21   globalsRegistered = true
     22 }
     23 function registerGlobalHandlers() {
     24   // When the window resizes, we need to refresh active editors.
     25   let resizeTimer
     26   on(window, "resize", () => {
     27     if (resizeTimer == null) resizeTimer = setTimeout(() => {
     28       resizeTimer = null
     29       forEachCodeMirror(onResize)
     30     }, 100)
     31   })
     32   // When the window loses focus, we want to show the editor as blurred
     33   on(window, "blur", () => forEachCodeMirror(onBlur))
     34 }
     35 // Called when the window resizes
     36 function onResize(cm) {
     37   let d = cm.display
     38   if (d.lastWrapHeight == d.wrapper.clientHeight && d.lastWrapWidth == d.wrapper.clientWidth)
     39     return
     40   // Might be a text scaling operation, clear size caches.
     41   d.cachedCharWidth = d.cachedTextHeight = d.cachedPaddingH = null
     42   d.scrollbarsClipped = false
     43   cm.setSize()
     44 }