edit.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /* global $ $create messageBoxProxy waitForSheet */// dom.js
  2. /* global msg API */// msg.js
  3. /* global CodeMirror */
  4. /* global SectionsEditor */
  5. /* global SourceEditor */
  6. /* global baseInit */
  7. /* global clipString createHotkeyInput helpPopup */// util.js
  8. /* global closeCurrentTab deepEqual sessionStore tryJSONparse */// toolbox.js
  9. /* global cmFactory */
  10. /* global editor */
  11. /* global linterMan */
  12. /* global prefs */
  13. /* global t */// localization.js
  14. /* global StyleSettings */// settings.js
  15. 'use strict';
  16. //#region init
  17. baseInit.ready.then(async () => {
  18. await waitForSheet();
  19. (editor.isUsercss ? SourceEditor : SectionsEditor)();
  20. StyleSettings(editor);
  21. await editor.ready;
  22. editor.ready = true;
  23. editor.dirty.onChange(editor.updateDirty);
  24. prefs.subscribe('editor.linter', (key, value) => {
  25. document.body.classList.toggle('linter-disabled', value === '');
  26. linterMan.run();
  27. });
  28. // enabling after init to prevent flash of validation failure on an empty name
  29. $('#name').required = !editor.isUsercss;
  30. $('#save-button').onclick = editor.save;
  31. const elSec = $('#sections-list');
  32. // editor.toc.expanded pref isn't saved in compact-layout so prefs.subscribe won't work
  33. if (elSec.open) editor.updateToc();
  34. // and we also toggle `open` directly in other places e.g. in detectLayout()
  35. new MutationObserver(() => elSec.open && editor.updateToc())
  36. .observe(elSec, {attributes: true, attributeFilter: ['open']});
  37. $('#toc').onclick = e =>
  38. editor.jumpToEditor([...$('#toc').children].indexOf(e.target));
  39. $('#keyMap-help').onclick = () =>
  40. require(['/edit/show-keymap-help'], () => showKeymapHelp()); /* global showKeymapHelp */
  41. $('#linter-settings').onclick = () =>
  42. require(['/edit/linter-dialogs'], () => linterMan.showLintConfig());
  43. $('#lint-help').onclick = () =>
  44. require(['/edit/linter-dialogs'], () => linterMan.showLintHelp());
  45. require([
  46. '/edit/autocomplete',
  47. '/edit/global-search',
  48. ]);
  49. });
  50. //#endregion
  51. //#region events
  52. const IGNORE_UPDATE_REASONS = [
  53. 'editPreview',
  54. 'editPreviewEnd',
  55. 'editSave',
  56. // https://github.com/openstyles/stylus/issues/807 is closed without fix
  57. // 'config,
  58. ];
  59. msg.onExtension(request => {
  60. const {style} = request;
  61. switch (request.method) {
  62. case 'styleUpdated':
  63. if (editor.style.id === style.id && !IGNORE_UPDATE_REASONS.includes(request.reason)) {
  64. if (request.reason === 'toggle') {
  65. editor.emit('styleToggled', request.style);
  66. } else {
  67. API.styles.get(request.style.id)
  68. .then(style => {
  69. editor.emit('styleChange', style, request.reason);
  70. });
  71. }
  72. }
  73. break;
  74. case 'styleDeleted':
  75. if (editor.style.id === style.id) {
  76. closeCurrentTab();
  77. }
  78. break;
  79. case 'editDeleteText':
  80. document.execCommand('delete');
  81. break;
  82. }
  83. });
  84. window.on('beforeunload', e => {
  85. let pos;
  86. if (editor.isWindowed &&
  87. document.visibilityState === 'visible' &&
  88. prefs.get('openEditInWindow') &&
  89. ( // only if not maximized
  90. screenX > 0 || outerWidth < screen.availWidth ||
  91. screenY > 0 || outerHeight < screen.availHeight ||
  92. screenX <= -10 || outerWidth >= screen.availWidth + 10 ||
  93. screenY <= -10 || outerHeight >= screen.availHeight + 10
  94. )
  95. ) {
  96. pos = {
  97. left: screenX,
  98. top: screenY,
  99. width: outerWidth,
  100. height: outerHeight,
  101. };
  102. prefs.set('windowPosition', pos);
  103. }
  104. sessionStore.windowPos = JSON.stringify(pos || {});
  105. sessionStore['editorScrollInfo' + editor.style.id] = JSON.stringify({
  106. scrollY: window.scrollY,
  107. cms: editor.getEditors().map(cm => /** @namespace EditorScrollInfo */({
  108. bookmarks: (cm.state.sublimeBookmarks || []).map(b => b.find()),
  109. focus: cm.hasFocus(),
  110. height: cm.display.wrapper.style.height.replace('100vh', ''),
  111. parentHeight: cm.display.wrapper.parentElement.offsetHeight,
  112. sel: cm.isClean() && [cm.doc.sel.ranges, cm.doc.sel.primIndex],
  113. })),
  114. });
  115. const activeElement = document.activeElement;
  116. if (activeElement) {
  117. // blurring triggers 'change' or 'input' event if needed
  118. activeElement.blur();
  119. // refocus if unloading was canceled
  120. setTimeout(() => activeElement.focus());
  121. }
  122. if (editor.dirty.isDirty()) {
  123. // neither confirm() nor custom messages work in modern browsers but just in case
  124. e.returnValue = t('styleChangesNotSaved');
  125. }
  126. });
  127. //#endregion
  128. //#region editor methods
  129. (() => {
  130. const toc = [];
  131. const {dirty} = editor;
  132. let {style} = editor;
  133. let wasDirty = false;
  134. Object.defineProperties(editor, {
  135. scrollInfo: {
  136. get: () => style.id && tryJSONparse(sessionStore['editorScrollInfo' + style.id]) || {},
  137. },
  138. style: {
  139. get: () => style,
  140. set: val => (style = val),
  141. },
  142. });
  143. /** @namespace Editor */
  144. Object.assign(editor, {
  145. applyScrollInfo(cm, si = (editor.scrollInfo.cms || [])[0]) {
  146. if (si && si.sel) {
  147. const bmOpts = {sublimeBookmark: true, clearWhenEmpty: false}; // copied from sublime.js
  148. cm.operation(() => {
  149. cm.setSelections(...si.sel, {scroll: false});
  150. cm.scrollIntoView(cm.getCursor(), si.parentHeight / 2);
  151. cm.state.sublimeBookmarks = si.bookmarks.map(b => cm.markText(b.from, b.to, bmOpts));
  152. });
  153. }
  154. },
  155. toggleStyle(enabled = style.enabled) {
  156. $('#enabled').checked = enabled;
  157. editor.updateEnabledness(enabled);
  158. },
  159. updateDirty() {
  160. const isDirty = dirty.isDirty();
  161. if (wasDirty !== isDirty) {
  162. wasDirty = isDirty;
  163. document.body.classList.toggle('dirty', isDirty);
  164. $('#save-button').disabled = !isDirty;
  165. }
  166. editor.updateTitle();
  167. },
  168. updateEnabledness(enabled) {
  169. dirty.modify('enabled', style.enabled, enabled);
  170. style.enabled = enabled;
  171. editor.updateLivePreview();
  172. },
  173. updateName(isUserInput) {
  174. if (!editor) return;
  175. if (isUserInput) {
  176. const {value} = $('#name');
  177. dirty.modify('name', style[editor.nameTarget] || style.name, value);
  178. style[editor.nameTarget] = value;
  179. }
  180. editor.updateTitle();
  181. },
  182. updateToc(added = editor.sections) {
  183. if (!toc.el) {
  184. toc.el = $('#toc');
  185. toc.elDetails = toc.el.closest('details');
  186. }
  187. if (!toc.elDetails.open) return;
  188. const {sections} = editor;
  189. const first = sections.indexOf(added[0]);
  190. const elFirst = toc.el.children[first];
  191. if (first >= 0 && (!added.focus || !elFirst)) {
  192. for (let el = elFirst, i = first; i < sections.length; i++) {
  193. const entry = sections[i].tocEntry;
  194. if (!deepEqual(entry, toc[i])) {
  195. if (!el) el = toc.el.appendChild($create('li', {tabIndex: 0}));
  196. el.tabIndex = entry.removed ? -1 : 0;
  197. toc[i] = Object.assign({}, entry);
  198. const s = el.textContent = clipString(entry.label) || (
  199. entry.target == null
  200. ? t('appliesToEverything')
  201. : clipString(entry.target) + (entry.numTargets > 1 ? ', ...' : ''));
  202. if (s.length > 30) el.title = s;
  203. }
  204. el = el.nextElementSibling;
  205. }
  206. }
  207. while (toc.length > sections.length) {
  208. toc.el.lastElementChild.remove();
  209. toc.length--;
  210. }
  211. if (added.focus) {
  212. const cls = 'current';
  213. const old = $('.' + cls, toc.el);
  214. const el = elFirst || toc.el.children[first];
  215. if (old && old !== el) old.classList.remove(cls);
  216. el.classList.add(cls);
  217. }
  218. },
  219. });
  220. })();
  221. //#endregion
  222. //#region editor livePreview
  223. editor.livePreview = (() => {
  224. let data;
  225. let port;
  226. let preprocess;
  227. let enabled = prefs.get('editor.livePreview');
  228. prefs.subscribe('editor.livePreview', (key, value) => {
  229. if (!value) {
  230. if (port) {
  231. port.disconnect();
  232. port = null;
  233. }
  234. } else if (data && data.id && (data.enabled || editor.dirty.has('enabled'))) {
  235. createPreviewer();
  236. updatePreviewer(data);
  237. }
  238. enabled = value;
  239. });
  240. return {
  241. /**
  242. * @param {Function} [fn] - preprocessor
  243. */
  244. init(fn) {
  245. preprocess = fn;
  246. },
  247. update(newData) {
  248. data = newData;
  249. if (!port) {
  250. if (!data.id || !data.enabled || !enabled) {
  251. return;
  252. }
  253. createPreviewer();
  254. }
  255. updatePreviewer(data);
  256. },
  257. };
  258. function createPreviewer() {
  259. port = chrome.runtime.connect({name: 'livePreview'});
  260. port.onDisconnect.addListener(err => {
  261. throw err;
  262. });
  263. }
  264. async function updatePreviewer(data) {
  265. const errorContainer = $('#preview-errors');
  266. try {
  267. port.postMessage(preprocess ? await preprocess(data) : data);
  268. errorContainer.classList.add('hidden');
  269. } catch (err) {
  270. if (Array.isArray(err)) {
  271. err = err.join('\n');
  272. } else if (err && err.index != null) {
  273. // FIXME: this would fail if editors[0].getValue() !== data.sourceCode
  274. const pos = editor.getEditors()[0].posFromIndex(err.index);
  275. err.message = `${pos.line}:${pos.ch} ${err.message || err}`;
  276. }
  277. errorContainer.classList.remove('hidden');
  278. errorContainer.onclick = () => {
  279. messageBoxProxy.alert(err.message || `${err}`, 'pre');
  280. };
  281. }
  282. }
  283. })();
  284. //#endregion
  285. //#region colorpickerHelper
  286. (async function colorpickerHelper() {
  287. prefs.subscribe('editor.colorpicker.hotkey', (id, hotkey) => {
  288. CodeMirror.commands.colorpicker = invokeColorpicker;
  289. const extraKeys = CodeMirror.defaults.extraKeys;
  290. for (const key in extraKeys) {
  291. if (extraKeys[key] === 'colorpicker') {
  292. delete extraKeys[key];
  293. break;
  294. }
  295. }
  296. if (hotkey) {
  297. extraKeys[hotkey] = 'colorpicker';
  298. }
  299. });
  300. prefs.subscribe('editor.colorpicker', (id, enabled) => {
  301. const defaults = CodeMirror.defaults;
  302. const keyName = prefs.get('editor.colorpicker.hotkey');
  303. defaults.colorpicker = enabled;
  304. if (enabled) {
  305. if (keyName) {
  306. CodeMirror.commands.colorpicker = invokeColorpicker;
  307. defaults.extraKeys = defaults.extraKeys || {};
  308. defaults.extraKeys[keyName] = 'colorpicker';
  309. }
  310. defaults.colorpicker = {
  311. tooltip: t('colorpickerTooltip'),
  312. popup: {
  313. tooltipForSwitcher: t('colorpickerSwitchFormatTooltip'),
  314. paletteLine: t('numberedLine'),
  315. paletteHint: t('colorpickerPaletteHint'),
  316. hexUppercase: prefs.get('editor.colorpicker.hexUppercase'),
  317. embedderCallback: state => {
  318. ['hexUppercase', 'color']
  319. .filter(name => state[name] !== prefs.get('editor.colorpicker.' + name))
  320. .forEach(name => prefs.set('editor.colorpicker.' + name, state[name]));
  321. },
  322. get maxHeight() {
  323. return prefs.get('editor.colorpicker.maxHeight');
  324. },
  325. set maxHeight(h) {
  326. prefs.set('editor.colorpicker.maxHeight', h);
  327. },
  328. },
  329. };
  330. } else {
  331. if (defaults.extraKeys) {
  332. delete defaults.extraKeys[keyName];
  333. }
  334. }
  335. cmFactory.globalSetOption('colorpicker', defaults.colorpicker);
  336. }, {runNow: true});
  337. await baseInit.domReady;
  338. $('#colorpicker-settings').onclick = function (event) {
  339. event.preventDefault();
  340. const input = createHotkeyInput('editor.colorpicker.hotkey', {onDone: () => helpPopup.close()});
  341. const popup = helpPopup.show(t('helpKeyMapHotkey'), input);
  342. const bounds = this.getBoundingClientRect();
  343. popup.style.left = bounds.right + 10 + 'px';
  344. popup.style.top = bounds.top - popup.clientHeight / 2 + 'px';
  345. popup.style.right = 'auto';
  346. $('input', popup).focus();
  347. };
  348. function invokeColorpicker(cm) {
  349. cm.state.colorpicker.openPopup(prefs.get('editor.colorpicker.color'));
  350. }
  351. })();
  352. //#endregion