1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | var tslib_1 = require("tslib");
|
4 | exports.InjectionMode = {
|
5 | |
6 |
|
7 |
|
8 | none: 0,
|
9 | |
10 |
|
11 |
|
12 | insertNode: 1,
|
13 | |
14 |
|
15 |
|
16 | appendChild: 2,
|
17 | };
|
18 | var STYLESHEET_SETTING = '__stylesheet__';
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | var REUSE_STYLE_NODE = typeof navigator !== 'undefined' && /rv:11.0/.test(navigator.userAgent);
|
24 | var _global = {};
|
25 |
|
26 | try {
|
27 | _global = window;
|
28 | }
|
29 | catch (_a) {
|
30 |
|
31 | }
|
32 | var _stylesheet;
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | var Stylesheet = (function () {
|
41 | function Stylesheet(config) {
|
42 | this._rules = [];
|
43 | this._preservedRules = [];
|
44 | this._rulesToInsert = [];
|
45 | this._counter = 0;
|
46 | this._keyToClassName = {};
|
47 | this._onResetCallbacks = [];
|
48 | this._classNameToArgs = {};
|
49 | this._config = tslib_1.__assign({ injectionMode: exports.InjectionMode.insertNode, defaultPrefix: 'css', namespace: undefined, cspSettings: undefined }, config);
|
50 | this._keyToClassName = this._config.classNameCache || {};
|
51 | }
|
52 | |
53 |
|
54 |
|
55 | Stylesheet.getInstance = function () {
|
56 | var _a;
|
57 | _stylesheet = _global[STYLESHEET_SETTING];
|
58 | if (!_stylesheet || (_stylesheet._lastStyleElement && _stylesheet._lastStyleElement.ownerDocument !== document)) {
|
59 | var fabricConfig = ((_a = _global) === null || _a === void 0 ? void 0 : _a.FabricConfig) || {};
|
60 | _stylesheet = _global[STYLESHEET_SETTING] = new Stylesheet(fabricConfig.mergeStyles);
|
61 | }
|
62 | return _stylesheet;
|
63 | };
|
64 | |
65 |
|
66 |
|
67 | Stylesheet.prototype.setConfig = function (config) {
|
68 | this._config = tslib_1.__assign(tslib_1.__assign({}, this._config), config);
|
69 | };
|
70 | |
71 |
|
72 |
|
73 |
|
74 |
|
75 | Stylesheet.prototype.onReset = function (callback) {
|
76 | this._onResetCallbacks.push(callback);
|
77 | };
|
78 | |
79 |
|
80 |
|
81 |
|
82 |
|
83 | Stylesheet.prototype.getClassName = function (displayName) {
|
84 | var namespace = this._config.namespace;
|
85 | var prefix = displayName || this._config.defaultPrefix;
|
86 | return "" + (namespace ? namespace + '-' : '') + prefix + "-" + this._counter++;
|
87 | };
|
88 | |
89 |
|
90 |
|
91 |
|
92 | Stylesheet.prototype.cacheClassName = function (className, key, args, rules) {
|
93 | this._keyToClassName[key] = className;
|
94 | this._classNameToArgs[className] = {
|
95 | args: args,
|
96 | rules: rules,
|
97 | };
|
98 | };
|
99 | |
100 |
|
101 |
|
102 |
|
103 | Stylesheet.prototype.classNameFromKey = function (key) {
|
104 | return this._keyToClassName[key];
|
105 | };
|
106 | |
107 |
|
108 |
|
109 | Stylesheet.prototype.getClassNameCache = function () {
|
110 | return this._keyToClassName;
|
111 | };
|
112 | |
113 |
|
114 |
|
115 |
|
116 | Stylesheet.prototype.argsFromClassName = function (className) {
|
117 | var entry = this._classNameToArgs[className];
|
118 | return entry && entry.args;
|
119 | };
|
120 | |
121 |
|
122 |
|
123 |
|
124 | Stylesheet.prototype.insertedRulesFromClassName = function (className) {
|
125 | var entry = this._classNameToArgs[className];
|
126 | return entry && entry.rules;
|
127 | };
|
128 | |
129 |
|
130 |
|
131 |
|
132 | Stylesheet.prototype.insertRule = function (rule, preserve) {
|
133 | var injectionMode = this._config.injectionMode;
|
134 | var element = injectionMode !== exports.InjectionMode.none ? this._getStyleElement() : undefined;
|
135 | if (preserve) {
|
136 | this._preservedRules.push(rule);
|
137 | }
|
138 | if (element) {
|
139 | switch (this._config.injectionMode) {
|
140 | case exports.InjectionMode.insertNode:
|
141 | var sheet = element.sheet;
|
142 | try {
|
143 | sheet.insertRule(rule, sheet.cssRules.length);
|
144 | }
|
145 | catch (e) {
|
146 |
|
147 |
|
148 |
|
149 | }
|
150 | break;
|
151 | case exports.InjectionMode.appendChild:
|
152 | element.appendChild(document.createTextNode(rule));
|
153 | break;
|
154 | }
|
155 | }
|
156 | else {
|
157 | this._rules.push(rule);
|
158 | }
|
159 | if (this._config.onInsertRule) {
|
160 | this._config.onInsertRule(rule);
|
161 | }
|
162 | };
|
163 | |
164 |
|
165 |
|
166 |
|
167 | Stylesheet.prototype.getRules = function (includePreservedRules) {
|
168 | return ((includePreservedRules ? this._preservedRules.join('') : '') + this._rules.join('') + this._rulesToInsert.join(''));
|
169 | };
|
170 | |
171 |
|
172 |
|
173 |
|
174 | Stylesheet.prototype.reset = function () {
|
175 | this._rules = [];
|
176 | this._rulesToInsert = [];
|
177 | this._counter = 0;
|
178 | this._classNameToArgs = {};
|
179 | this._keyToClassName = {};
|
180 | this._onResetCallbacks.forEach(function (callback) { return callback(); });
|
181 | };
|
182 |
|
183 | Stylesheet.prototype.resetKeys = function () {
|
184 | this._keyToClassName = {};
|
185 | };
|
186 | Stylesheet.prototype._getStyleElement = function () {
|
187 | var _this = this;
|
188 | if (!this._styleElement && typeof document !== 'undefined') {
|
189 | this._styleElement = this._createStyleElement();
|
190 | if (!REUSE_STYLE_NODE) {
|
191 |
|
192 | window.requestAnimationFrame(function () {
|
193 | _this._styleElement = undefined;
|
194 | });
|
195 | }
|
196 | }
|
197 | return this._styleElement;
|
198 | };
|
199 | Stylesheet.prototype._createStyleElement = function () {
|
200 | var head = document.head;
|
201 | var styleElement = document.createElement('style');
|
202 | styleElement.setAttribute('data-merge-styles', 'true');
|
203 | var cspSettings = this._config.cspSettings;
|
204 | if (cspSettings) {
|
205 | if (cspSettings.nonce) {
|
206 | styleElement.setAttribute('nonce', cspSettings.nonce);
|
207 | }
|
208 | }
|
209 | if (this._lastStyleElement) {
|
210 |
|
211 |
|
212 | head.insertBefore(styleElement, this._lastStyleElement.nextElementSibling);
|
213 | }
|
214 | else {
|
215 | var placeholderStyleTag = this._findPlaceholderStyleTag();
|
216 | if (placeholderStyleTag) {
|
217 | head.insertBefore(styleElement, placeholderStyleTag.nextElementSibling);
|
218 | }
|
219 | else {
|
220 | head.insertBefore(styleElement, head.childNodes[0]);
|
221 | }
|
222 | }
|
223 | this._lastStyleElement = styleElement;
|
224 | return styleElement;
|
225 | };
|
226 | Stylesheet.prototype._findPlaceholderStyleTag = function () {
|
227 | var head = document.head;
|
228 | if (head) {
|
229 | return head.querySelector('style[data-merge-styles]');
|
230 | }
|
231 | return null;
|
232 | };
|
233 | return Stylesheet;
|
234 | }());
|
235 | exports.Stylesheet = Stylesheet;
|
236 |
|
\ | No newline at end of file |