codemirror-default.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* global CodeMirror prefs loadScript editor */
  2. 'use strict';
  3. (function () {
  4. // CodeMirror miserably fails on keyMap='' so let's ensure it's not
  5. if (!prefs.get('editor.keyMap')) {
  6. prefs.reset('editor.keyMap');
  7. }
  8. const defaults = {
  9. mode: 'css',
  10. lineNumbers: true,
  11. lineWrapping: true,
  12. foldGutter: true,
  13. gutters: [
  14. 'CodeMirror-linenumbers',
  15. 'CodeMirror-foldgutter',
  16. ...(prefs.get('editor.linter') ? ['CodeMirror-lint-markers'] : []),
  17. ],
  18. matchBrackets: true,
  19. highlightSelectionMatches: {showToken: /[#.\-\w]/, annotateScrollbar: true},
  20. hintOptions: {},
  21. lintReportDelay: prefs.get('editor.lintReportDelay'),
  22. styleActiveLine: true,
  23. theme: 'default',
  24. keyMap: prefs.get('editor.keyMap'),
  25. extraKeys: {
  26. // independent of current keyMap
  27. 'Alt-Enter': 'toggleStyle',
  28. 'Alt-PageDown': 'nextEditor',
  29. 'Alt-PageUp': 'prevEditor'
  30. }
  31. };
  32. Object.assign(CodeMirror.defaults, defaults, prefs.get('editor.options'));
  33. CodeMirror.commands.blockComment = cm => {
  34. cm.blockComment(cm.getCursor('from'), cm.getCursor('to'), {fullLines: false});
  35. };
  36. // 'basic' keymap only has basic keys by design, so we skip it
  37. const extraKeysCommands = {};
  38. Object.keys(CodeMirror.defaults.extraKeys).forEach(key => {
  39. extraKeysCommands[CodeMirror.defaults.extraKeys[key]] = true;
  40. });
  41. if (!extraKeysCommands.jumpToLine) {
  42. CodeMirror.keyMap.sublime['Ctrl-G'] = 'jumpToLine';
  43. CodeMirror.keyMap.emacsy['Ctrl-G'] = 'jumpToLine';
  44. CodeMirror.keyMap.pcDefault['Ctrl-J'] = 'jumpToLine';
  45. CodeMirror.keyMap.macDefault['Cmd-J'] = 'jumpToLine';
  46. }
  47. if (!extraKeysCommands.autocomplete) {
  48. // will be used by 'sublime' on PC via fallthrough
  49. CodeMirror.keyMap.pcDefault['Ctrl-Space'] = 'autocomplete';
  50. // OSX uses Ctrl-Space and Cmd-Space for something else
  51. CodeMirror.keyMap.macDefault['Alt-Space'] = 'autocomplete';
  52. // copied from 'emacs' keymap
  53. CodeMirror.keyMap.emacsy['Alt-/'] = 'autocomplete';
  54. // 'vim' and 'emacs' define their own autocomplete hotkeys
  55. }
  56. if (!extraKeysCommands.blockComment) {
  57. CodeMirror.keyMap.sublime['Shift-Ctrl-/'] = 'blockComment';
  58. }
  59. if (navigator.appVersion.includes('Windows')) {
  60. // 'pcDefault' keymap on Windows should have F3/Shift-F3
  61. if (!extraKeysCommands.findNext) {
  62. CodeMirror.keyMap.pcDefault['F3'] = 'findNext';
  63. }
  64. if (!extraKeysCommands.findPrev) {
  65. CodeMirror.keyMap.pcDefault['Shift-F3'] = 'findPrev';
  66. }
  67. // try to remap non-interceptable Ctrl-(Shift-)N/T/W hotkeys
  68. ['N', 'T', 'W'].forEach(char => {
  69. [
  70. {from: 'Ctrl-', to: ['Alt-', 'Ctrl-Alt-']},
  71. // Note: modifier order in CodeMirror is S-C-A
  72. {from: 'Shift-Ctrl-', to: ['Ctrl-Alt-', 'Shift-Ctrl-Alt-']}
  73. ].forEach(remap => {
  74. const oldKey = remap.from + char;
  75. Object.keys(CodeMirror.keyMap).forEach(keyMapName => {
  76. const keyMap = CodeMirror.keyMap[keyMapName];
  77. const command = keyMap[oldKey];
  78. if (!command) {
  79. return;
  80. }
  81. remap.to.some(newMod => {
  82. const newKey = newMod + char;
  83. if (!(newKey in keyMap)) {
  84. delete keyMap[oldKey];
  85. keyMap[newKey] = command;
  86. return true;
  87. }
  88. });
  89. });
  90. });
  91. });
  92. }
  93. Object.assign(CodeMirror.mimeModes['text/css'].propertyKeywords, {
  94. 'mix-blend-mode': true,
  95. 'isolation': true,
  96. });
  97. Object.assign(CodeMirror.mimeModes['text/css'].valueKeywords, {
  98. 'isolate': true,
  99. });
  100. const MODE = {
  101. stylus: 'stylus',
  102. uso: 'css'
  103. };
  104. CodeMirror.defineExtension('setPreprocessor', function (preprocessor) {
  105. const mode = MODE[preprocessor] || 'css';
  106. return loadScript(mode !== 'css' && `/vendor/codemirror/mode/${mode}/${mode}.js`).then(() => {
  107. this.setOption('mode', mode);
  108. });
  109. });
  110. CodeMirror.defineExtension('isBlank', function () {
  111. // superfast checking as it runs only until the first non-blank line
  112. let isBlank = true;
  113. this.doc.eachLine(line => {
  114. if (line.text && line.text.trim()) {
  115. isBlank = false;
  116. return true;
  117. }
  118. });
  119. return isBlank;
  120. });
  121. })();
  122. // eslint-disable-next-line no-unused-expressions
  123. CodeMirror.hint && (() => {
  124. const USO_VAR = 'uso-variable';
  125. const USO_VALID_VAR = 'variable-3 ' + USO_VAR;
  126. const USO_INVALID_VAR = 'error ' + USO_VAR;
  127. const originalHelper = CodeMirror.hint.css || (() => {});
  128. CodeMirror.registerHelper('hint', 'css', function (cm) {
  129. const {line, ch} = cm.getCursor();
  130. const {styles, text} = cm.getLineHandle(line);
  131. if (!styles || !editor) {
  132. return originalHelper(cm);
  133. }
  134. let prev = 0;
  135. for (let i = 1; i < styles.length; i += 2) {
  136. let end = styles[i];
  137. if (prev <= ch && ch <= end &&
  138. (styles[i + 1] || '').includes(USO_VAR)) {
  139. const adjust = text[prev] === '/' ? 4 : 0;
  140. prev += adjust;
  141. end -= adjust;
  142. const leftPart = text.slice(prev, ch);
  143. const list = Object.keys(editor.getStyle().usercssData.vars)
  144. .filter(name => name.startsWith(leftPart));
  145. return {
  146. list,
  147. from: {line, ch: prev},
  148. to: {line, ch: end},
  149. };
  150. }
  151. prev = end;
  152. }
  153. return originalHelper(cm);
  154. });
  155. const hooks = CodeMirror.mimeModes['text/css'].tokenHooks;
  156. const originalCommentHook = hooks['/'];
  157. hooks['/'] = tokenizeUsoVariables;
  158. function tokenizeUsoVariables(stream) {
  159. const token = originalCommentHook.apply(this, arguments);
  160. if (token[1] !== 'comment') {
  161. return token;
  162. }
  163. const {string, start, pos} = stream;
  164. // /*[[install-key]]*/
  165. // 01234 43210
  166. if (string[start + 2] === '[' &&
  167. string[start + 3] === '[' &&
  168. string[pos - 3] === ']' &&
  169. string[pos - 4] === ']') {
  170. if (typeof editor !== 'undefined' &&
  171. Object.hasOwnProperty.call(
  172. editor.getStyle().usercssData.vars,
  173. string.slice(start + 4, pos - 4))) {
  174. token[0] = USO_VALID_VAR;
  175. } else {
  176. token[0] = USO_INVALID_VAR;
  177. }
  178. }
  179. return token;
  180. }
  181. })();