aceEditor.js 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  1. import _ from 'lodash'
  2. /* eslint-disable
  3. camelcase,
  4. max-len
  5. */
  6. import App from '../../../base'
  7. import UndoManager from './aceEditor/undo/UndoManager'
  8. import AutoCompleteManager from './aceEditor/auto-complete/AutoCompleteManager'
  9. import SpellCheckManager from './aceEditor/spell-check/SpellCheckManager'
  10. import SpellCheckAdapter from './aceEditor/spell-check/SpellCheckAdapter'
  11. import HighlightsManager from './aceEditor/highlights/HighlightsManager'
  12. import CursorPositionManager from './aceEditor/cursor-position/CursorPositionManager'
  13. import CursorPositionAdapter from './aceEditor/cursor-position/CursorPositionAdapter'
  14. import TrackChangesManager from './aceEditor/track-changes/TrackChangesManager'
  15. import TrackChangesAdapter from './aceEditor/track-changes/TrackChangesAdapter'
  16. import MetadataManager from './aceEditor/metadata/MetadataManager'
  17. import 'ace/ace'
  18. import 'ace/ext-searchbox'
  19. import 'ace/ext-modelist'
  20. import 'ace/keybinding-vim'
  21. import '../../metadata/services/metadata'
  22. import '../../graphics/services/graphics'
  23. import '../../preamble/services/preamble'
  24. import '../../files/services/files'
  25. let syntaxValidationEnabled
  26. const { EditSession } = ace.require('ace/edit_session')
  27. const ModeList = ace.require('ace/ext/modelist')
  28. const { Vim } = ace.require('ace/keyboard/vim')
  29. const SearchBox = ace.require('ace/ext/searchbox')
  30. // Set the base path that ace will fetch modes/snippets/workers from
  31. if (window.aceBasePath !== '') {
  32. syntaxValidationEnabled = true
  33. ace.config.set('basePath', `${window.aceBasePath}`)
  34. } else {
  35. syntaxValidationEnabled = false
  36. }
  37. // By default, don't use workers - enable them per-session as required
  38. ace.config.setDefaultValue('session', 'useWorker', false)
  39. // Ace loads its script itself, so we need to hook in to be able to clear
  40. // the cache.
  41. if (ace.config._moduleUrl == null) {
  42. ace.config._moduleUrl = ace.config.moduleUrl
  43. ace.config.moduleUrl = function(...args) {
  44. const url = ace.config._moduleUrl(...Array.from(args || []))
  45. return url
  46. }
  47. }
  48. App.directive('aceEditor', function(
  49. $timeout,
  50. $compile,
  51. $rootScope,
  52. eventTracking,
  53. localStorage,
  54. $cacheFactory,
  55. metadata,
  56. graphics,
  57. preamble,
  58. files,
  59. $http,
  60. $q,
  61. $window
  62. ) {
  63. monkeyPatchSearch($rootScope, $compile)
  64. return {
  65. scope: {
  66. theme: '=',
  67. showPrintMargin: '=',
  68. keybindings: '=',
  69. fontSize: '=',
  70. autoComplete: '=',
  71. autoPairDelimiters: '=',
  72. sharejsDoc: '=',
  73. spellCheck: '=',
  74. spellCheckLanguage: '=',
  75. highlights: '=',
  76. text: '=',
  77. readOnly: '=',
  78. annotations: '=',
  79. navigateHighlights: '=',
  80. fileName: '=',
  81. onCtrlEnter: '=', // Compile
  82. onCtrlJ: '=', // Toggle the review panel
  83. onCtrlShiftC: '=', // Add a new comment
  84. onCtrlShiftA: '=', // Toggle track-changes on/off
  85. onSave: '=', // Cmd/Ctrl-S or :w in Vim
  86. syntaxValidation: '=',
  87. reviewPanel: '=',
  88. eventsBridge: '=',
  89. trackChanges: '=',
  90. docId: '=',
  91. rendererData: '=',
  92. lineHeight: '=',
  93. fontFamily: '='
  94. },
  95. link(scope, element, attrs) {
  96. // Don't freak out if we're already in an apply callback
  97. let spellCheckManager
  98. scope.$originalApply = scope.$apply
  99. scope.$apply = function(fn) {
  100. if (fn == null) {
  101. fn = function() {}
  102. }
  103. const phase = this.$root.$$phase
  104. if (phase === '$apply' || phase === '$digest') {
  105. return fn()
  106. } else {
  107. return this.$originalApply(fn)
  108. }
  109. }
  110. const editor = ace.edit(element.find('.ace-editor-body')[0])
  111. editor.$blockScrolling = Infinity
  112. // auto-insertion of braces, brackets, dollars
  113. editor.setOption('behavioursEnabled', scope.autoPairDelimiters || false)
  114. editor.setOption('wrapBehavioursEnabled', false)
  115. scope.$watch('autoPairDelimiters', autoPairDelimiters => {
  116. if (autoPairDelimiters) {
  117. return editor.setOption('behavioursEnabled', true)
  118. } else {
  119. return editor.setOption('behavioursEnabled', false)
  120. }
  121. })
  122. if (!window._debug_editors) {
  123. window._debug_editors = []
  124. }
  125. window._debug_editors.push(editor)
  126. scope.name = attrs.aceEditor
  127. if (scope.spellCheck) {
  128. // only enable spellcheck when explicitly required
  129. spellCheckManager = new SpellCheckManager(
  130. scope,
  131. $cacheFactory,
  132. $http,
  133. $q,
  134. new SpellCheckAdapter(editor)
  135. )
  136. }
  137. /* eslint-disable no-unused-vars */
  138. const undoManager = new UndoManager(editor)
  139. const highlightsManager = new HighlightsManager(scope, editor, element)
  140. const cursorPositionManager = new CursorPositionManager(
  141. scope,
  142. new CursorPositionAdapter(editor),
  143. localStorage
  144. )
  145. const trackChangesManager = new TrackChangesManager(
  146. scope,
  147. editor,
  148. element,
  149. new TrackChangesAdapter(editor)
  150. )
  151. const metadataManager = new MetadataManager(
  152. scope,
  153. editor,
  154. element,
  155. metadata
  156. )
  157. const autoCompleteManager = new AutoCompleteManager(
  158. scope,
  159. editor,
  160. element,
  161. metadataManager,
  162. graphics,
  163. preamble,
  164. files
  165. )
  166. // prevent user entering null and non-BMP unicode characters in Ace
  167. const BAD_CHARS_REGEXP = /[\0\uD800-\uDFFF]/g
  168. const BAD_CHARS_REPLACEMENT_CHAR = '\uFFFD'
  169. // the 'exec' event fires for ace functions before they are executed.
  170. // you can modify the input or reject the event with e.preventDefault()
  171. editor.commands.on('exec', function(e) {
  172. // replace bad characters in paste content
  173. if (e.command && e.command.name === 'paste') {
  174. BAD_CHARS_REGEXP.lastIndex = 0 // reset stateful regexp for this usage
  175. if (e.args && BAD_CHARS_REGEXP.test(e.args.text)) {
  176. e.args.text = e.args.text.replace(
  177. BAD_CHARS_REGEXP,
  178. BAD_CHARS_REPLACEMENT_CHAR
  179. )
  180. }
  181. }
  182. // replace bad characters in keyboard input
  183. if (e.command && e.command.name === 'insertstring') {
  184. BAD_CHARS_REGEXP.lastIndex = 0 // reset stateful regexp for this usage
  185. if (e.args && BAD_CHARS_REGEXP.test(e.args)) {
  186. e.args = e.args.replace(
  187. BAD_CHARS_REGEXP,
  188. BAD_CHARS_REPLACEMENT_CHAR
  189. )
  190. }
  191. }
  192. })
  193. /* eslint-enable no-unused-vars */
  194. scope.$watch('onSave', function(callback) {
  195. if (callback != null) {
  196. Vim.defineEx('write', 'w', callback)
  197. editor.commands.addCommand({
  198. name: 'save',
  199. bindKey: {
  200. win: 'Ctrl-S',
  201. mac: 'Command-S'
  202. },
  203. exec: callback,
  204. readOnly: true
  205. })
  206. // Not technically 'save', but Ctrl-. recompiles in OL v1
  207. // so maintain compatibility
  208. return editor.commands.addCommand({
  209. name: 'recompile_v1',
  210. bindKey: {
  211. win: 'Ctrl-.',
  212. mac: 'Ctrl-.'
  213. },
  214. exec: callback,
  215. readOnly: true
  216. })
  217. }
  218. })
  219. editor.commands.removeCommand('transposeletters')
  220. editor.commands.removeCommand('showSettingsMenu')
  221. editor.commands.removeCommand('foldall')
  222. // For European keyboards, the / is above 7 so needs Shift pressing.
  223. // This comes through as Command-Shift-/ on OS X, which is mapped to
  224. // toggleBlockComment.
  225. // This doesn't do anything for LaTeX, so remap this to togglecomment to
  226. // work for European keyboards as normal.
  227. // On Windows, the key combo comes as Ctrl-Shift-7.
  228. editor.commands.removeCommand('toggleBlockComment')
  229. editor.commands.removeCommand('togglecomment')
  230. editor.commands.addCommand({
  231. name: 'togglecomment',
  232. bindKey: {
  233. win: 'Ctrl-/|Ctrl-Shift-7',
  234. mac: 'Command-/|Command-Shift-/'
  235. },
  236. exec(editor) {
  237. return editor.toggleCommentLines()
  238. },
  239. multiSelectAction: 'forEachLine',
  240. scrollIntoView: 'selectionPart'
  241. })
  242. // Trigger search AND replace on CMD+F
  243. editor.commands.addCommand({
  244. name: 'find',
  245. bindKey: {
  246. win: 'Ctrl-F',
  247. mac: 'Command-F'
  248. },
  249. exec(editor) {
  250. return SearchBox.Search(editor, true)
  251. },
  252. readOnly: true
  253. })
  254. // Bold text on CMD+B
  255. editor.commands.addCommand({
  256. name: 'bold',
  257. bindKey: {
  258. win: 'Ctrl-B',
  259. mac: 'Command-B'
  260. },
  261. exec(editor) {
  262. const selection = editor.getSelection()
  263. if (selection.isEmpty()) {
  264. editor.insert('\\textbf{}')
  265. return editor.navigateLeft(1)
  266. } else {
  267. const text = editor.getCopyText()
  268. return editor.insert(`\\textbf{${text}}`)
  269. }
  270. },
  271. readOnly: false
  272. })
  273. // Italicise text on CMD+I
  274. editor.commands.addCommand({
  275. name: 'italics',
  276. bindKey: {
  277. win: 'Ctrl-I',
  278. mac: 'Command-I'
  279. },
  280. exec(editor) {
  281. const selection = editor.getSelection()
  282. if (selection.isEmpty()) {
  283. editor.insert('\\textit{}')
  284. return editor.navigateLeft(1)
  285. } else {
  286. const text = editor.getCopyText()
  287. return editor.insert(`\\textit{${text}}`)
  288. }
  289. },
  290. readOnly: false
  291. })
  292. scope.$watch('onCtrlEnter', function(callback) {
  293. if (callback != null) {
  294. return editor.commands.addCommand({
  295. name: 'compile',
  296. bindKey: {
  297. win: 'Ctrl-Enter',
  298. mac: 'Command-Enter'
  299. },
  300. exec: editor => {
  301. return callback()
  302. },
  303. readOnly: true
  304. })
  305. }
  306. })
  307. scope.$watch('onCtrlJ', function(callback) {
  308. if (callback != null) {
  309. return editor.commands.addCommand({
  310. name: 'toggle-review-panel',
  311. bindKey: {
  312. win: 'Ctrl-J',
  313. mac: 'Command-J'
  314. },
  315. exec: editor => {
  316. return callback()
  317. },
  318. readOnly: true
  319. })
  320. }
  321. })
  322. scope.$watch('onCtrlShiftC', function(callback) {
  323. if (callback != null) {
  324. return editor.commands.addCommand({
  325. name: 'add-new-comment',
  326. bindKey: {
  327. win: 'Ctrl-Shift-C',
  328. mac: 'Command-Shift-C'
  329. },
  330. exec: editor => {
  331. return callback()
  332. },
  333. readOnly: true
  334. })
  335. }
  336. })
  337. scope.$watch('onCtrlShiftA', function(callback) {
  338. if (callback != null) {
  339. return editor.commands.addCommand({
  340. name: 'toggle-track-changes',
  341. bindKey: {
  342. win: 'Ctrl-Shift-A',
  343. mac: 'Command-Shift-A'
  344. },
  345. exec: editor => {
  346. return callback()
  347. },
  348. readOnly: true
  349. })
  350. }
  351. })
  352. // Make '/' work for search in vim mode.
  353. editor.showCommandLine = arg => {
  354. if (arg === '/') {
  355. return SearchBox.Search(editor, true)
  356. }
  357. }
  358. const getCursorScreenPosition = function() {
  359. const session = editor.getSession()
  360. const cursorPosition = session.selection.getCursor()
  361. const sessionPos = session.documentToScreenPosition(
  362. cursorPosition.row,
  363. cursorPosition.column
  364. )
  365. return (
  366. sessionPos.row * editor.renderer.lineHeight - session.getScrollTop()
  367. )
  368. }
  369. if (attrs.resizeOn != null) {
  370. for (let event of Array.from(attrs.resizeOn.split(','))) {
  371. scope.$on(event, function() {
  372. scope.$applyAsync(() => {
  373. const previousScreenPosition = getCursorScreenPosition()
  374. editor.resize()
  375. // Put cursor back to same vertical position on screen
  376. const newScreenPosition = getCursorScreenPosition()
  377. const session = editor.getSession()
  378. return session.setScrollTop(
  379. session.getScrollTop() +
  380. newScreenPosition -
  381. previousScreenPosition
  382. )
  383. })
  384. })
  385. }
  386. }
  387. scope.$on(`${scope.name}:set-scroll-size`, function(e, size) {
  388. // Make sure that the editor has enough scroll margin above and below
  389. // to scroll the review panel with the given size
  390. const marginTop = size.overflowTop
  391. const { maxHeight } = editor.renderer.layerConfig
  392. const marginBottom = Math.max(size.height - maxHeight, 0)
  393. return setScrollMargins(marginTop, marginBottom)
  394. })
  395. var setScrollMargins = function(marginTop, marginBottom) {
  396. let marginChanged = false
  397. if (editor.renderer.scrollMargin.top !== marginTop) {
  398. editor.renderer.scrollMargin.top = marginTop
  399. marginChanged = true
  400. }
  401. if (editor.renderer.scrollMargin.bottom !== marginBottom) {
  402. editor.renderer.scrollMargin.bottom = marginBottom
  403. marginChanged = true
  404. }
  405. if (marginChanged) {
  406. return editor.renderer.updateFull()
  407. }
  408. }
  409. const resetScrollMargins = () => setScrollMargins(0, 0)
  410. scope.$watch('theme', value => editor.setTheme(`ace/theme/${value}`))
  411. scope.$watch('showPrintMargin', value => editor.setShowPrintMargin(value))
  412. scope.$watch('keybindings', function(value) {
  413. if (['vim', 'emacs'].includes(value)) {
  414. return editor.setKeyboardHandler(`ace/keyboard/${value}`)
  415. } else {
  416. return editor.setKeyboardHandler(null)
  417. }
  418. })
  419. scope.$watch('fontSize', value =>
  420. element.find('.ace_editor, .ace_content').css({
  421. 'font-size': value + 'px'
  422. })
  423. )
  424. scope.$watch('fontFamily', function(value) {
  425. const monospaceFamilies = [
  426. 'Monaco',
  427. 'Menlo',
  428. 'Ubuntu Mono',
  429. 'Consolas',
  430. 'source-code-pro',
  431. 'monospace'
  432. ]
  433. if (value != null) {
  434. switch (value) {
  435. case 'monaco':
  436. return editor.setOption(
  437. 'fontFamily',
  438. monospaceFamilies.join(', ')
  439. )
  440. case 'lucida':
  441. return editor.setOption(
  442. 'fontFamily',
  443. '"Lucida Console", monospace'
  444. )
  445. default:
  446. return editor.setOption('fontFamily', null)
  447. }
  448. }
  449. })
  450. scope.$watch('lineHeight', function(value) {
  451. if (value != null) {
  452. switch (value) {
  453. case 'compact':
  454. editor.container.style.lineHeight = 1.33
  455. break
  456. case 'normal':
  457. editor.container.style.lineHeight = 1.6
  458. break
  459. case 'wide':
  460. editor.container.style.lineHeight = 2
  461. break
  462. default:
  463. editor.container.style.lineHeight = 1.6
  464. }
  465. return editor.renderer.updateFontSize()
  466. }
  467. })
  468. scope.$watch('sharejsDoc', function(sharejs_doc, old_sharejs_doc) {
  469. if (old_sharejs_doc != null) {
  470. scope.$broadcast('beforeChangeDocument')
  471. detachFromAce(old_sharejs_doc)
  472. }
  473. if (sharejs_doc != null) {
  474. attachToAce(sharejs_doc)
  475. }
  476. if (sharejs_doc != null && old_sharejs_doc != null) {
  477. return scope.$broadcast('afterChangeDocument')
  478. }
  479. })
  480. scope.$watch('text', function(text) {
  481. if (text != null) {
  482. editor.setValue(text, -1)
  483. const session = editor.getSession()
  484. return session.setUseWrapMode(true)
  485. }
  486. })
  487. scope.$watch('annotations', function(annotations) {
  488. const session = editor.getSession()
  489. return session.setAnnotations(annotations)
  490. })
  491. scope.$watch('readOnly', value => editor.setReadOnly(!!value))
  492. scope.$watch('syntaxValidation', function(value) {
  493. // ignore undefined settings here
  494. // only instances of ace with an explicit value should set useWorker
  495. // the history instance will have syntaxValidation undefined
  496. if (value != null && syntaxValidationEnabled) {
  497. const session = editor.getSession()
  498. return session.setOption('useWorker', value)
  499. }
  500. })
  501. editor.setOption('scrollPastEnd', true)
  502. let updateCount = 0
  503. const onChange = function() {
  504. updateCount++
  505. if (updateCount === 100) {
  506. eventTracking.send('editor-interaction', 'multi-doc-update')
  507. }
  508. return scope.$emit(`${scope.name}:change`)
  509. }
  510. const onScroll = function(scrollTop) {
  511. if (scope.eventsBridge == null) {
  512. return
  513. }
  514. const height = editor.renderer.layerConfig.maxHeight
  515. return scope.eventsBridge.emit('aceScroll', scrollTop, height)
  516. }
  517. const onScrollbarVisibilityChanged = function(event, vRenderer) {
  518. if (scope.eventsBridge == null) {
  519. return
  520. }
  521. return scope.eventsBridge.emit(
  522. 'aceScrollbarVisibilityChanged',
  523. vRenderer.scrollBarV.isVisible,
  524. vRenderer.scrollBarV.width
  525. )
  526. }
  527. if (scope.eventsBridge != null) {
  528. editor.renderer.on(
  529. 'scrollbarVisibilityChanged',
  530. onScrollbarVisibilityChanged
  531. )
  532. scope.eventsBridge.on('externalScroll', position =>
  533. editor.getSession().setScrollTop(position)
  534. )
  535. scope.eventsBridge.on('refreshScrollPosition', function() {
  536. const session = editor.getSession()
  537. session.setScrollTop(session.getScrollTop() + 1)
  538. return session.setScrollTop(session.getScrollTop() - 1)
  539. })
  540. }
  541. const onSessionChangeForSpellCheck = function(e) {
  542. spellCheckManager.onSessionChange()
  543. if (e.oldSession != null) {
  544. e.oldSession.getDocument().off('change', spellCheckManager.onChange)
  545. }
  546. e.session.getDocument().on('change', spellCheckManager.onChange)
  547. if (e.oldSession != null) {
  548. e.oldSession.off('changeScrollTop', spellCheckManager.onScroll)
  549. }
  550. return e.session.on('changeScrollTop', spellCheckManager.onScroll)
  551. }
  552. const initSpellCheck = function() {
  553. if (!spellCheckManager) return
  554. spellCheckManager.init()
  555. editor.on('changeSession', onSessionChangeForSpellCheck)
  556. onSessionChangeForSpellCheck({
  557. session: editor.getSession()
  558. }) // Force initial setup
  559. return editor.on('nativecontextmenu', spellCheckManager.onContextMenu)
  560. }
  561. const tearDownSpellCheck = function() {
  562. if (!spellCheckManager) return
  563. editor.off('changeSession', onSessionChangeForSpellCheck)
  564. return editor.off('nativecontextmenu', spellCheckManager.onContextMenu)
  565. }
  566. const initTrackChanges = function() {
  567. trackChangesManager.rangesTracker = scope.sharejsDoc.ranges
  568. // Force onChangeSession in order to set up highlights etc.
  569. trackChangesManager.onChangeSession()
  570. if (!trackChangesManager) return
  571. editor.on('changeSelection', trackChangesManager.onChangeSelection)
  572. // Selection also moves with updates elsewhere in the document
  573. editor.on('change', trackChangesManager.onChangeSelection)
  574. editor.on('changeSession', trackChangesManager.onChangeSession)
  575. editor.on('cut', trackChangesManager.onCut)
  576. editor.on('paste', trackChangesManager.onPaste)
  577. editor.renderer.on('resize', trackChangesManager.onResize)
  578. }
  579. const tearDownTrackChanges = function() {
  580. if (!trackChangesManager) return
  581. trackChangesManager.tearDown()
  582. editor.off('changeSelection', trackChangesManager.onChangeSelection)
  583. editor.off('change', trackChangesManager.onChangeSelection)
  584. editor.off('changeSession', trackChangesManager.onChangeSession)
  585. editor.off('cut', trackChangesManager.onCut)
  586. editor.off('paste', trackChangesManager.onPaste)
  587. editor.renderer.off('resize', trackChangesManager.onResize)
  588. }
  589. const initUndo = function() {
  590. // Emulate onChangeSession event. Note: listening to changeSession
  591. // event is unnecessary since this method is called when we switch
  592. // sessions (via ShareJS changing) anyway
  593. undoManager.onChangeSession(editor.getSession())
  594. editor.on('change', undoManager.onChange)
  595. }
  596. const tearDownUndo = function() {
  597. editor.off('change', undoManager.onChange)
  598. }
  599. const onSessionChangeForCursorPosition = function(e) {
  600. if (e.oldSession != null) {
  601. e.oldSession.selection.off(
  602. 'changeCursor',
  603. cursorPositionManager.onCursorChange
  604. )
  605. }
  606. return e.session.selection.on(
  607. 'changeCursor',
  608. cursorPositionManager.onCursorChange
  609. )
  610. }
  611. const onUnloadForCursorPosition = () =>
  612. cursorPositionManager.onUnload(editor.getSession())
  613. const initCursorPosition = function() {
  614. editor.on('changeSession', onSessionChangeForCursorPosition)
  615. // Force initial setup
  616. onSessionChangeForCursorPosition({ session: editor.getSession() })
  617. return $(window).on('unload', onUnloadForCursorPosition)
  618. }
  619. const tearDownCursorPosition = function() {
  620. editor.off('changeSession', onSessionChangeForCursorPosition)
  621. return $(window).off('unload', onUnloadForCursorPosition)
  622. }
  623. initCursorPosition()
  624. // Trigger the event once *only* - this is called after Ace is connected
  625. // to the ShareJs instance but this event should only be triggered the
  626. // first time the editor is opened. Not every time the docs opened
  627. const triggerEditorInitEvent = _.once(() =>
  628. scope.$broadcast('editorInit')
  629. )
  630. var attachToAce = function(sharejs_doc) {
  631. let mode
  632. const lines = sharejs_doc.getSnapshot().split('\n')
  633. let session = editor.getSession()
  634. if (session != null) {
  635. session.destroy()
  636. }
  637. // see if we can lookup a suitable mode from ace
  638. // but fall back to text by default
  639. try {
  640. if (/\.(Rtex|bbl|tikz)$/i.test(scope.fileName)) {
  641. // recognise Rtex and bbl as latex
  642. mode = 'ace/mode/latex'
  643. } else if (/\.(sty|cls|clo)$/.test(scope.fileName)) {
  644. // recognise some common files as tex
  645. mode = 'ace/mode/tex'
  646. } else {
  647. ;({ mode } = ModeList.getModeForPath(scope.fileName))
  648. // we prefer plain_text mode over text mode because ace's
  649. // text mode is actually for code and has unwanted
  650. // indenting (see wrapMethod in ace edit_session.js)
  651. if (mode === 'ace/mode/text') {
  652. mode = 'ace/mode/plain_text'
  653. }
  654. }
  655. } catch (error) {
  656. mode = 'ace/mode/plain_text'
  657. }
  658. // create our new session
  659. session = new EditSession(lines, mode)
  660. session.setUseWrapMode(true)
  661. // use syntax validation only when explicitly set
  662. if (
  663. scope.syntaxValidation != null &&
  664. syntaxValidationEnabled &&
  665. !/\.bib$/.test(scope.fileName)
  666. ) {
  667. session.setOption('useWorker', scope.syntaxValidation)
  668. }
  669. // set to readonly until document change handlers are attached
  670. editor.setReadOnly(true)
  671. // now attach session to editor
  672. editor.setSession(session)
  673. const doc = session.getDocument()
  674. doc.on('change', onChange)
  675. editor.initing = true
  676. sharejs_doc.attachToAce(editor)
  677. editor.initing = false
  678. // now ready to edit document
  679. // respect the readOnly setting, normally false
  680. editor.setReadOnly(scope.readOnly)
  681. triggerEditorInitEvent()
  682. if (!scope.readOnly) {
  683. initSpellCheck()
  684. }
  685. initTrackChanges()
  686. initUndo()
  687. resetScrollMargins()
  688. // need to set annotations after attaching because attaching
  689. // deletes and then inserts document content
  690. session.setAnnotations(scope.annotations)
  691. session.on('changeScrollTop', eventTracking.editingSessionHeartbeat)
  692. angular
  693. .element($window)
  694. .on('click', eventTracking.editingSessionHeartbeat)
  695. scope.$on('$destroy', () =>
  696. angular
  697. .element($window)
  698. .off('click', eventTracking.editingSessionHeartbeat)
  699. )
  700. if (scope.eventsBridge != null) {
  701. session.on('changeScrollTop', onScroll)
  702. }
  703. $rootScope.hasLintingError = false
  704. session.on('changeAnnotation', function() {
  705. // Both linter errors and compile logs are set as error annotations,
  706. // however when the user types something, the compile logs are
  707. // replaced with linter errors. When we check for lint errors before
  708. // autocompile we are guaranteed to get linter errors
  709. const hasErrors =
  710. session
  711. .getAnnotations()
  712. .filter(annotation => annotation.type !== 'info').length > 0
  713. if ($rootScope.hasLintingError !== hasErrors) {
  714. return ($rootScope.hasLintingError = hasErrors)
  715. }
  716. })
  717. setTimeout(() =>
  718. // Let any listeners init themselves
  719. onScroll(editor.renderer.getScrollTop())
  720. )
  721. return editor.focus()
  722. }
  723. var detachFromAce = function(sharejs_doc) {
  724. tearDownSpellCheck()
  725. tearDownTrackChanges()
  726. tearDownUndo()
  727. sharejs_doc.detachFromAce()
  728. sharejs_doc.off('remoteop.recordRemote')
  729. const session = editor.getSession()
  730. session.off('changeScrollTop')
  731. const doc = session.getDocument()
  732. return doc.off('change', onChange)
  733. }
  734. if (scope.rendererData != null) {
  735. editor.renderer.on('changeCharacterSize', () => {
  736. scope.$apply(
  737. () => (scope.rendererData.lineHeight = editor.renderer.lineHeight)
  738. )
  739. })
  740. }
  741. scope.$watch('rendererData', function(rendererData) {
  742. if (rendererData != null) {
  743. return (rendererData.lineHeight = editor.renderer.lineHeight)
  744. }
  745. })
  746. scope.$on('$destroy', function() {
  747. if (scope.sharejsDoc != null) {
  748. scope.$broadcast('changeEditor')
  749. tearDownSpellCheck()
  750. tearDownCursorPosition()
  751. tearDownUndo()
  752. detachFromAce(scope.sharejsDoc)
  753. const session = editor.getSession()
  754. if (session != null) {
  755. session.destroy()
  756. }
  757. return scope.eventsBridge.emit(
  758. 'aceScrollbarVisibilityChanged',
  759. false,
  760. 0
  761. )
  762. }
  763. })
  764. return scope.$emit(`${scope.name}:inited`, editor)
  765. },
  766. template: `\
  767. <div class="ace-editor-wrapper">
  768. <div
  769. class="undo-conflict-warning alert alert-danger small"
  770. ng-show="undo.show_remote_warning"
  771. >
  772. <strong>Watch out!</strong>
  773. We had to undo some of your collaborators changes before we could undo yours.
  774. <a
  775. href="#"
  776. class="pull-right"
  777. ng-click="undo.show_remote_warning = false"
  778. >Dismiss</a>
  779. </div>
  780. <div class="ace-editor-body"></div>
  781. <spell-menu
  782. open="spellMenu.open"
  783. top="spellMenu.top"
  784. left="spellMenu.left"
  785. layout-from-bottom="spellMenu.layoutFromBottom"
  786. highlight="spellMenu.highlight"
  787. replace-word="replaceWord(highlight, suggestion)"
  788. learn-word="learnWord(highlight)"
  789. ></spell-menu>
  790. <div
  791. class="annotation-label"
  792. ng-show="annotationLabel.show"
  793. ng-style="{
  794. position: 'absolute',
  795. left: annotationLabel.left,
  796. right: annotationLabel.right,
  797. bottom: annotationLabel.bottom,
  798. top: annotationLabel.top,
  799. 'background-color': annotationLabel.backgroundColor
  800. }"
  801. >
  802. {{ annotationLabel.text }}
  803. </div>
  804. <a
  805. href
  806. class="highlights-before-label btn btn-info btn-xs"
  807. ng-show="updateLabels.highlightsBefore > 0"
  808. ng-click="gotoHighlightAbove()"
  809. >
  810. <i class="fa fa-fw fa-arrow-up"></i>
  811. {{ updateLabels.highlightsBefore }} more update{{ updateLabels.highlightsBefore > 1 && "" || "s" }} above
  812. </a>
  813. <a
  814. href
  815. class="highlights-after-label btn btn-info btn-xs"
  816. ng-show="updateLabels.highlightsAfter > 0"
  817. ng-click="gotoHighlightBelow()"
  818. >
  819. <i class="fa fa-fw fa-arrow-down"></i>
  820. {{ updateLabels.highlightsAfter }} more update{{ updateLabels.highlightsAfter > 1 && "" || "s" }} below
  821. </a>
  822. </div>\
  823. `
  824. }
  825. })
  826. function monkeyPatchSearch($rootScope, $compile) {
  827. const searchHtml = `\
  828. <div class="ace_search right">
  829. <a href type="button" action="hide" class="ace_searchbtn_close">
  830. <i class="fa fa-fw fa-times"></i>
  831. </a>
  832. <div class="ace_search_form">
  833. <input class="ace_search_field form-control input-sm" placeholder="Search for" spellcheck="false"></input>
  834. <div class="btn-group">
  835. <button type="button" action="findNext" class="ace_searchbtn next btn btn-default btn-sm">
  836. <i class="fa fa-chevron-down fa-fw"></i>
  837. </button>
  838. <button type="button" action="findPrev" class="ace_searchbtn prev btn btn-default btn-sm">
  839. <i class="fa fa-chevron-up fa-fw"></i>
  840. </button>
  841. </div>
  842. </div>
  843. <div class="ace_replace_form">
  844. <input class="ace_search_field form-control input-sm" placeholder="Replace with" spellcheck="false"></input>
  845. <div class="btn-group">
  846. <button type="button" action="replaceAndFindNext" class="ace_replacebtn btn btn-default btn-sm">Replace</button>
  847. <button type="button" action="replaceAll" class="ace_replacebtn btn btn-default btn-sm">All</button>
  848. </div>
  849. </div>
  850. <div class="ace_search_options">
  851. <div class="btn-group">
  852. <button action="toggleRegexpMode" class="btn btn-default btn-sm" tooltip-placement="bottom" tooltip-append-to-body="true" tooltip="RegExp Search">.*</button>
  853. <button action="toggleCaseSensitive" class="btn btn-default btn-sm" tooltip-placement="bottom" tooltip-append-to-body="true" tooltip="CaseSensitive Search">Aa</button>
  854. <button action="toggleWholeWords" class="btn btn-default btn-sm" tooltip-placement="bottom" tooltip-append-to-body="true" tooltip="Whole Word Search">"..."</button>
  855. <button action="searchInSelection" class="btn btn-default btn-sm" tooltip-placement="bottom" tooltip-append-to-body="true" tooltip="Search Within Selection"><i class="fa fa-align-left"></i></button>
  856. </div>
  857. <span class="ace_search_counter"></span>
  858. </div>
  859. <div action="toggleReplace" class="hidden"></div>
  860. </div>\
  861. `
  862. // Remove Ace CSS
  863. $('#ace_searchbox').remove()
  864. const SB = SearchBox.SearchBox
  865. const { $init } = SB.prototype
  866. SB.prototype.$init = function() {
  867. this.element = $compile(searchHtml)($rootScope.$new())[0]
  868. return $init.apply(this)
  869. }
  870. }