global.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Handles using MathJax global as config object, and to hold
  19. * methods and data to be available to the page author
  20. *
  21. * @author dpvc@mathjax.org (Davide Cervone)
  22. */
  23. import {VERSION} from './version.js';
  24. /**
  25. * The MathJax variable as a configuration object
  26. */
  27. export interface MathJaxConfig {
  28. [name: string]: any;
  29. }
  30. /**
  31. * The object used to store class and other definitions
  32. * from the various MathJax modules so that they can be shared
  33. * among the various component webpack files
  34. */
  35. export interface MathJaxLibrary {
  36. [name: string]: any;
  37. }
  38. /**
  39. * The MathJax global object structure
  40. */
  41. export interface MathJaxObject {
  42. version: string;
  43. _: MathJaxLibrary;
  44. config: MathJaxConfig;
  45. }
  46. declare const global: {MathJax: MathJaxObject | MathJaxConfig};
  47. /**
  48. * @param {any} x An item to test if it is an object
  49. * @return {boolean} True if the item is a non-null object
  50. */
  51. export function isObject(x: any): boolean {
  52. return typeof x === 'object' && x !== null;
  53. }
  54. /**
  55. * Combine user-produced configuration with existing defaults. Values
  56. * from src will replace those in dst.
  57. *
  58. * @param {any} dst The destination config object (to be merged into)
  59. * @param {any} src The source configuration object (to replace defaul values in dst}
  60. * @return {any} The resulting (modified) config object
  61. */
  62. export function combineConfig(dst: any, src: any): any {
  63. for (const id of Object.keys(src)) {
  64. if (id === '__esModule') continue;
  65. if (isObject(dst[id]) && isObject(src[id]) &&
  66. !(src[id] instanceof Promise) /* needed for IE polyfill */) {
  67. combineConfig(dst[id], src[id]);
  68. } else if (src[id] !== null && src[id] !== undefined) {
  69. dst[id] = src[id];
  70. }
  71. }
  72. return dst;
  73. }
  74. /**
  75. * Combine defaults into a configuration that may already have
  76. * user-provided values. Values in src only go into dst if
  77. * there is not already a value for that key.
  78. *
  79. * @param {any} dst The destination config object (to be merged into)
  80. * @param {string} name The id of the configuration block to modify (created if doesn't exist)
  81. * @param {any} src The source configuration object (to replace defaul values in dst}
  82. * @return {any} The resulting (modified) config object
  83. */
  84. export function combineDefaults(dst: any, name: string, src: any): any {
  85. if (!dst[name]) {
  86. dst[name] = {};
  87. }
  88. dst = dst[name];
  89. for (const id of Object.keys(src)) {
  90. if (isObject(dst[id]) && isObject(src[id])) {
  91. combineDefaults(dst, id, src[id]);
  92. } else if (dst[id] == null && src[id] != null) {
  93. dst[id] = src[id];
  94. }
  95. }
  96. return dst;
  97. }
  98. /**
  99. * Combine configuration or data with the existing MathJax object
  100. *
  101. * @param {any} config The data to be merged into the MathJax object
  102. */
  103. export function combineWithMathJax(config: any): MathJaxObject {
  104. return combineConfig(MathJax, config);
  105. }
  106. /**
  107. * Create the MathJax global, if it doesn't exist
  108. */
  109. if (typeof global.MathJax === 'undefined') {
  110. global.MathJax = {} as MathJaxConfig;
  111. }
  112. /**
  113. * If the global is currently a config object, convert it to the
  114. * MathJaxObject containing the version, class library, and user
  115. * configuration.
  116. */
  117. if (!(global.MathJax as MathJaxObject).version) {
  118. global.MathJax = {
  119. version: VERSION,
  120. _: {},
  121. config: global.MathJax
  122. };
  123. }
  124. /**
  125. * Export the global MathJax object for convenience
  126. */
  127. export const MathJax = global.MathJax as MathJaxObject;