2017-03-08 13:41:05 -05:00
|
|
|
/* eslint-env browser, jquery */
|
2019-05-30 18:27:56 -04:00
|
|
|
/* eslint no-console: ["error", { allow: ["warn", "error", "debug"] }] */
|
2021-02-12 17:39:39 -05:00
|
|
|
/* global CodeMirror, Cookies, moment, Idle, serverurl,
|
2019-02-20 19:36:26 -05:00
|
|
|
key, Dropbox, ot, hex2rgb, Visibility */
|
2016-10-07 11:06:10 -04:00
|
|
|
|
2018-11-21 05:11:47 -05:00
|
|
|
import TurndownService from 'turndown'
|
2016-10-07 11:06:10 -04:00
|
|
|
|
2017-03-28 06:31:36 -04:00
|
|
|
import { saveAs } from 'file-saver'
|
|
|
|
import randomColor from 'randomcolor'
|
2018-03-23 10:39:16 -04:00
|
|
|
import store from 'store'
|
2018-10-10 05:13:44 -04:00
|
|
|
import hljs from 'highlight.js'
|
2019-04-16 13:27:02 -04:00
|
|
|
import url from 'wurl'
|
2021-02-12 17:39:39 -05:00
|
|
|
import { Spinner } from 'spin.js'
|
2016-10-12 23:42:17 -04:00
|
|
|
|
2017-03-28 06:31:36 -04:00
|
|
|
import _ from 'lodash'
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2017-03-28 06:31:36 -04:00
|
|
|
import List from 'list.js'
|
2016-10-12 20:56:56 -04:00
|
|
|
|
2017-01-05 03:48:23 -05:00
|
|
|
import {
|
2019-05-30 18:27:56 -04:00
|
|
|
checkLoginStateChanged,
|
|
|
|
setloginStateChangeEvent
|
2017-03-08 13:41:05 -05:00
|
|
|
} from './lib/common/login'
|
2017-01-13 09:51:44 -05:00
|
|
|
|
|
|
|
import {
|
2019-05-30 18:27:56 -04:00
|
|
|
debug,
|
|
|
|
DROPBOX_APP_KEY,
|
|
|
|
noteid,
|
|
|
|
noteurl,
|
|
|
|
urlpath,
|
|
|
|
version
|
2017-03-08 13:41:05 -05:00
|
|
|
} from './lib/config'
|
2016-10-08 08:02:30 -04:00
|
|
|
|
2017-01-05 04:52:32 -05:00
|
|
|
import {
|
2019-05-30 18:27:56 -04:00
|
|
|
autoLinkify,
|
|
|
|
deduplicatedHeaderId,
|
|
|
|
exportToHTML,
|
|
|
|
exportToRawHTML,
|
|
|
|
removeDOMEvents,
|
|
|
|
finishView,
|
|
|
|
generateToc,
|
|
|
|
isValidURL,
|
|
|
|
md,
|
|
|
|
parseMeta,
|
|
|
|
postProcess,
|
|
|
|
renderFilename,
|
|
|
|
renderTOC,
|
|
|
|
renderTags,
|
|
|
|
renderTitle,
|
|
|
|
scrollToHash,
|
|
|
|
smoothHashScroll,
|
|
|
|
updateLastChange,
|
|
|
|
updateLastChangeUser,
|
|
|
|
updateOwner
|
2017-03-08 13:41:05 -05:00
|
|
|
} from './extra'
|
2016-10-08 08:02:30 -04:00
|
|
|
|
2017-01-04 10:01:44 -05:00
|
|
|
import {
|
2019-05-30 18:27:56 -04:00
|
|
|
clearMap,
|
|
|
|
setupSyncAreas,
|
|
|
|
syncScrollToEdit,
|
|
|
|
syncScrollToView
|
2017-04-11 09:33:54 -04:00
|
|
|
} from './lib/syncscroll'
|
2016-10-13 12:02:55 -04:00
|
|
|
|
2017-01-05 07:56:16 -05:00
|
|
|
import {
|
2019-05-30 18:27:56 -04:00
|
|
|
writeHistory,
|
|
|
|
deleteServerHistory,
|
|
|
|
getHistory,
|
|
|
|
saveHistory,
|
|
|
|
removeHistory
|
2017-03-08 13:41:05 -05:00
|
|
|
} from './history'
|
2016-10-08 08:02:30 -04:00
|
|
|
|
2017-03-28 06:31:36 -04:00
|
|
|
import { preventXSS } from './render'
|
2016-10-07 11:06:10 -04:00
|
|
|
|
2017-03-13 09:32:50 -04:00
|
|
|
import Editor from './lib/editor'
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-13 09:32:50 -04:00
|
|
|
import getUIElements from './lib/editor/ui-elements'
|
2017-04-11 21:21:13 -04:00
|
|
|
import modeType from './lib/modeType'
|
|
|
|
import appState from './lib/appState'
|
2016-09-18 04:35:24 -04:00
|
|
|
|
2019-05-30 18:27:56 -04:00
|
|
|
require('../vendor/showup/showup')
|
|
|
|
|
|
|
|
require('../css/index.css')
|
|
|
|
require('../css/extra.css')
|
|
|
|
require('../css/slide-preview.css')
|
|
|
|
require('../css/site.css')
|
|
|
|
|
|
|
|
require('highlight.js/styles/github-gist.css')
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let defaultTextHeight = 20
|
|
|
|
let viewportMargin = 20
|
|
|
|
const defaultEditorMode = 'gfm'
|
|
|
|
|
|
|
|
const idleTime = 300000 // 5 mins
|
|
|
|
const updateViewDebounce = 100
|
|
|
|
const cursorMenuThrottle = 50
|
|
|
|
const cursorActivityDebounce = 50
|
|
|
|
const cursorAnimatePeriod = 100
|
|
|
|
const supportContainers = ['success', 'info', 'warning', 'danger']
|
|
|
|
const supportCodeModes = [
|
|
|
|
'javascript',
|
|
|
|
'typescript',
|
|
|
|
'jsx',
|
|
|
|
'htmlmixed',
|
|
|
|
'htmlembedded',
|
|
|
|
'css',
|
|
|
|
'xml',
|
|
|
|
'clike',
|
|
|
|
'clojure',
|
|
|
|
'ruby',
|
|
|
|
'python',
|
|
|
|
'shell',
|
|
|
|
'php',
|
|
|
|
'sql',
|
|
|
|
'haskell',
|
|
|
|
'coffeescript',
|
|
|
|
'yaml',
|
|
|
|
'pug',
|
|
|
|
'lua',
|
|
|
|
'cmake',
|
|
|
|
'nginx',
|
|
|
|
'perl',
|
|
|
|
'sass',
|
|
|
|
'r',
|
|
|
|
'dockerfile',
|
|
|
|
'tiddlywiki',
|
|
|
|
'mediawiki',
|
|
|
|
'go',
|
|
|
|
'gherkin'
|
|
|
|
].concat(hljs.listLanguages())
|
|
|
|
const supportCharts = ['sequence', 'flow', 'graphviz', 'mermaid', 'abc']
|
|
|
|
const supportHeaders = [
|
2017-03-08 13:41:05 -05:00
|
|
|
{
|
|
|
|
text: '# h1',
|
|
|
|
search: '#'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '## h2',
|
|
|
|
search: '##'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '### h3',
|
|
|
|
search: '###'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '#### h4',
|
|
|
|
search: '####'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '##### h5',
|
|
|
|
search: '#####'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '###### h6',
|
|
|
|
search: '######'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '###### tags: `example`',
|
|
|
|
search: '###### tags:'
|
|
|
|
}
|
|
|
|
]
|
2017-04-11 00:07:04 -04:00
|
|
|
const supportReferrals = [
|
2017-03-08 13:41:05 -05:00
|
|
|
{
|
|
|
|
text: '[reference link]',
|
|
|
|
search: '[]'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[reference]: https:// "title"',
|
|
|
|
search: '[]:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[^footnote link]',
|
|
|
|
search: '[^]'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[^footnote reference]: https:// "title"',
|
|
|
|
search: '[^]:'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '^[inline footnote]',
|
|
|
|
search: '^[]'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[link text][reference]',
|
|
|
|
search: '[][]'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[link text](https:// "title")',
|
|
|
|
search: '[]()'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '![image alt][reference]',
|
|
|
|
search: '![][]'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '![image alt](https:// "title")',
|
|
|
|
search: '![]()'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '![image alt](https:// "title" =WidthxHeight)',
|
|
|
|
search: '![]()'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[TOC]',
|
|
|
|
search: '[]'
|
|
|
|
}
|
|
|
|
]
|
2017-04-11 00:07:04 -04:00
|
|
|
const supportExternals = [
|
2017-03-08 13:41:05 -05:00
|
|
|
{
|
|
|
|
text: '{%youtube youtubeid %}',
|
|
|
|
search: 'youtube'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '{%vimeo vimeoid %}',
|
|
|
|
search: 'vimeo'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '{%gist gistid %}',
|
|
|
|
search: 'gist'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '{%slideshare slideshareid %}',
|
|
|
|
search: 'slideshare'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '{%speakerdeck speakerdeckid %}',
|
|
|
|
search: 'speakerdeck'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '{%pdf pdfurl %}',
|
|
|
|
search: 'pdf'
|
|
|
|
}
|
|
|
|
]
|
2017-04-11 00:07:04 -04:00
|
|
|
const supportExtraTags = [
|
2017-03-08 13:41:05 -05:00
|
|
|
{
|
|
|
|
text: '[name tag]',
|
|
|
|
search: '[]',
|
|
|
|
command: function () {
|
2017-04-11 00:07:04 -04:00
|
|
|
return '[name=' + personalInfo.name + ']'
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[time tag]',
|
|
|
|
search: '[]',
|
|
|
|
command: function () {
|
|
|
|
return '[time=' + moment().format('llll') + ']'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[my color tag]',
|
|
|
|
search: '[]',
|
|
|
|
command: function () {
|
2017-04-11 00:07:04 -04:00
|
|
|
return '[color=' + personalInfo.color + ']'
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: '[random color tag]',
|
|
|
|
search: '[]',
|
|
|
|
command: function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const color = randomColor()
|
2017-03-08 13:41:05 -05:00
|
|
|
return '[color=' + color + ']'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
2017-04-11 00:07:04 -04:00
|
|
|
const statusType = {
|
2017-03-08 13:41:05 -05:00
|
|
|
connected: {
|
|
|
|
msg: 'CONNECTED',
|
|
|
|
label: 'label-warning',
|
|
|
|
fa: 'fa-wifi'
|
|
|
|
},
|
|
|
|
online: {
|
|
|
|
msg: 'ONLINE',
|
|
|
|
label: 'label-primary',
|
|
|
|
fa: 'fa-users'
|
|
|
|
},
|
|
|
|
offline: {
|
|
|
|
msg: 'OFFLINE',
|
|
|
|
label: 'label-danger',
|
|
|
|
fa: 'fa-plug'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// global vars
|
|
|
|
window.loaded = false
|
2017-04-10 23:48:39 -04:00
|
|
|
let needRefresh = false
|
|
|
|
let isDirty = false
|
|
|
|
let editShown = false
|
|
|
|
let visibleXS = false
|
|
|
|
let visibleSM = false
|
|
|
|
let visibleMD = false
|
|
|
|
let visibleLG = false
|
|
|
|
const isTouchDevice = 'ontouchstart' in document.documentElement
|
2017-04-11 00:07:04 -04:00
|
|
|
let currentStatus = statusType.offline
|
2021-02-15 03:42:51 -05:00
|
|
|
const lastInfo = {
|
2017-03-08 13:41:05 -05:00
|
|
|
needRestore: false,
|
|
|
|
cursor: null,
|
|
|
|
scroll: null,
|
|
|
|
edit: {
|
|
|
|
scroll: {
|
|
|
|
left: null,
|
|
|
|
top: null
|
2015-05-04 03:53:29 -04:00
|
|
|
},
|
2017-03-08 13:41:05 -05:00
|
|
|
cursor: {
|
|
|
|
line: null,
|
|
|
|
ch: null
|
2015-05-04 03:53:29 -04:00
|
|
|
},
|
2017-03-08 13:41:05 -05:00
|
|
|
selections: null
|
|
|
|
},
|
|
|
|
view: {
|
|
|
|
scroll: {
|
|
|
|
left: null,
|
|
|
|
top: null
|
|
|
|
}
|
|
|
|
},
|
|
|
|
history: null
|
|
|
|
}
|
2017-04-11 00:07:04 -04:00
|
|
|
let personalInfo = {}
|
|
|
|
let onlineUsers = []
|
|
|
|
const fileTypes = {
|
2021-02-15 03:42:51 -05:00
|
|
|
pl: 'perl',
|
|
|
|
cgi: 'perl',
|
|
|
|
js: 'javascript',
|
|
|
|
php: 'php',
|
|
|
|
sh: 'bash',
|
|
|
|
rb: 'ruby',
|
|
|
|
html: 'html',
|
|
|
|
py: 'python'
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// editor settings
|
2017-04-11 00:07:04 -04:00
|
|
|
const textit = document.getElementById('textit')
|
2017-03-07 09:35:54 -05:00
|
|
|
if (!textit) {
|
2017-03-13 09:32:50 -04:00
|
|
|
throw new Error('There was no textit area!')
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
2017-03-13 09:32:50 -04:00
|
|
|
const editorInstance = new Editor()
|
2021-02-15 03:42:51 -05:00
|
|
|
const editor = editorInstance.init(textit)
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2017-03-27 23:17:30 -04:00
|
|
|
// FIXME: global referncing in jquery-textcomplete patch
|
2017-03-13 09:32:50 -04:00
|
|
|
window.editor = editor
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2017-03-13 09:32:50 -04:00
|
|
|
defaultTextHeight = parseInt($('.CodeMirror').css('line-height'))
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2017-03-07 09:35:54 -05:00
|
|
|
// initalize ui reference
|
2017-03-13 09:32:50 -04:00
|
|
|
const ui = getUIElements()
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// page actions
|
2021-02-15 03:42:51 -05:00
|
|
|
const opts = {
|
2017-03-08 13:41:05 -05:00
|
|
|
lines: 11, // The number of lines to draw
|
|
|
|
length: 20, // The length of each line
|
|
|
|
width: 2, // The line thickness
|
|
|
|
radius: 30, // The radius of the inner circle
|
|
|
|
corners: 0, // Corner roundness (0..1)
|
|
|
|
rotate: 0, // The rotation offset
|
|
|
|
direction: 1, // 1: clockwise, -1: counterclockwise
|
|
|
|
color: '#000', // #rgb or #rrggbb or array of colors
|
|
|
|
speed: 1.1, // Rounds per second
|
|
|
|
trail: 60, // Afterglow percentage
|
|
|
|
shadow: false, // Whether to render a shadow
|
|
|
|
hwaccel: true, // Whether to use hardware acceleration
|
|
|
|
className: 'spinner', // The CSS class to assign to the spinner
|
|
|
|
zIndex: 2e9, // The z-index (defaults to 2000000000)
|
|
|
|
top: '50%', // Top position relative to parent
|
|
|
|
left: '50%' // Left position relative to parent
|
|
|
|
}
|
|
|
|
|
|
|
|
/* eslint-disable no-unused-vars */
|
2021-02-15 03:42:51 -05:00
|
|
|
const spinner = new Spinner(opts).spin(ui.spinner[0])
|
2017-03-08 13:41:05 -05:00
|
|
|
/* eslint-enable no-unused-vars */
|
|
|
|
|
|
|
|
// idle
|
2021-02-15 03:42:51 -05:00
|
|
|
const idle = new Idle({
|
2017-03-08 13:41:05 -05:00
|
|
|
onAway: function () {
|
|
|
|
idle.isAway = true
|
|
|
|
emitUserStatus()
|
|
|
|
updateOnlineStatus()
|
|
|
|
},
|
|
|
|
onAwayBack: function () {
|
|
|
|
idle.isAway = false
|
|
|
|
emitUserStatus()
|
|
|
|
updateOnlineStatus()
|
|
|
|
setHaveUnreadChanges(false)
|
|
|
|
updateTitleReminder()
|
|
|
|
},
|
|
|
|
awayTimeout: idleTime
|
|
|
|
})
|
2015-06-01 06:04:25 -04:00
|
|
|
ui.area.codemirror.on('touchstart', function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
idle.onActive()
|
|
|
|
})
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let haveUnreadChanges = false
|
2015-09-25 06:48:45 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function setHaveUnreadChanges (bool) {
|
|
|
|
if (!window.loaded) return
|
|
|
|
if (bool && (idle.isAway || Visibility.hidden())) {
|
|
|
|
haveUnreadChanges = true
|
|
|
|
} else if (!bool && !idle.isAway && !Visibility.hidden()) {
|
|
|
|
haveUnreadChanges = false
|
|
|
|
}
|
2015-09-25 06:48:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function updateTitleReminder () {
|
|
|
|
if (!window.loaded) return
|
|
|
|
if (haveUnreadChanges) {
|
|
|
|
document.title = '• ' + renderTitle(ui.area.markdown)
|
|
|
|
} else {
|
|
|
|
document.title = renderTitle(ui.area.markdown)
|
|
|
|
}
|
2015-09-25 06:48:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function setRefreshModal (status) {
|
|
|
|
$('#refreshModal').modal('show')
|
|
|
|
$('#refreshModal').find('.modal-body > div').hide()
|
2021-02-15 03:42:51 -05:00
|
|
|
$('#refreshModal')
|
|
|
|
.find('.' + status)
|
|
|
|
.show()
|
2016-06-17 04:31:36 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function setNeedRefresh () {
|
2017-04-10 23:48:39 -04:00
|
|
|
needRefresh = true
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.setOption('readOnly', true)
|
|
|
|
socket.disconnect()
|
|
|
|
showStatus(statusType.offline)
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2017-01-05 03:48:23 -05:00
|
|
|
setloginStateChangeEvent(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
setRefreshModal('user-state-changed')
|
|
|
|
setNeedRefresh()
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// visibility
|
2021-02-15 03:42:51 -05:00
|
|
|
let wasFocus = false
|
2015-06-01 06:04:25 -04:00
|
|
|
Visibility.change(function (e, state) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const hidden = Visibility.hidden()
|
2017-03-08 13:41:05 -05:00
|
|
|
if (hidden) {
|
|
|
|
if (editorHasFocus()) {
|
|
|
|
wasFocus = true
|
|
|
|
editor.getInputField().blur()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (wasFocus) {
|
2017-04-10 23:48:39 -04:00
|
|
|
if (!visibleXS) {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.focus()
|
|
|
|
editor.refresh()
|
|
|
|
}
|
|
|
|
wasFocus = false
|
|
|
|
}
|
|
|
|
setHaveUnreadChanges(false)
|
|
|
|
}
|
|
|
|
updateTitleReminder()
|
|
|
|
})
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// when page ready
|
2015-05-04 03:53:29 -04:00
|
|
|
$(document).ready(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
idle.checkAway()
|
|
|
|
checkResponsive()
|
2019-05-30 18:27:56 -04:00
|
|
|
// if in smaller screen, we don't need advanced scrollbar
|
2021-02-15 03:42:51 -05:00
|
|
|
let scrollbarStyle
|
2017-04-10 23:48:39 -04:00
|
|
|
if (visibleXS) {
|
2017-03-08 13:41:05 -05:00
|
|
|
scrollbarStyle = 'native'
|
|
|
|
} else {
|
|
|
|
scrollbarStyle = 'overlay'
|
|
|
|
}
|
|
|
|
if (scrollbarStyle !== editor.getOption('scrollbarStyle')) {
|
|
|
|
editor.setOption('scrollbarStyle', scrollbarStyle)
|
|
|
|
clearMap()
|
|
|
|
}
|
|
|
|
checkEditorStyle()
|
2018-03-23 10:39:16 -04:00
|
|
|
|
|
|
|
/* cache dom references */
|
2021-02-15 03:42:51 -05:00
|
|
|
const $body = $('body')
|
2018-03-23 10:39:16 -04:00
|
|
|
|
2017-03-28 08:38:31 -04:00
|
|
|
/* we need this only on touch devices */
|
2017-04-10 23:48:39 -04:00
|
|
|
if (isTouchDevice) {
|
2017-03-28 08:38:31 -04:00
|
|
|
/* bind events */
|
2017-03-08 13:41:05 -05:00
|
|
|
$(document)
|
2019-05-30 18:27:56 -04:00
|
|
|
.on('focus', 'textarea, input', function () {
|
|
|
|
$body.addClass('fixfixed')
|
|
|
|
})
|
|
|
|
.on('blur', 'textarea, input', function () {
|
|
|
|
$body.removeClass('fixfixed')
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2018-03-23 10:39:16 -04:00
|
|
|
|
|
|
|
// Re-enable nightmode
|
|
|
|
if (store.get('nightMode') || Cookies.get('nightMode')) {
|
|
|
|
$body.addClass('night')
|
2018-03-25 14:11:53 -04:00
|
|
|
ui.toolbar.night.addClass('active')
|
2018-03-23 10:39:16 -04:00
|
|
|
}
|
|
|
|
|
2017-03-28 08:38:31 -04:00
|
|
|
// showup
|
2017-03-08 13:41:05 -05:00
|
|
|
$().showUp('.navbar', {
|
|
|
|
upClass: 'navbar-hide',
|
|
|
|
downClass: 'navbar-show'
|
|
|
|
})
|
2017-03-28 08:38:31 -04:00
|
|
|
// tooltip
|
2017-03-08 13:41:05 -05:00
|
|
|
$('[data-toggle="tooltip"]').tooltip()
|
2017-03-28 08:38:31 -04:00
|
|
|
// shortcuts
|
|
|
|
// allow on all tags
|
2021-02-15 03:42:51 -05:00
|
|
|
key.filter = function (e) {
|
|
|
|
return true
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
key('ctrl+alt+e', function (e) {
|
|
|
|
changeMode(modeType.edit)
|
|
|
|
})
|
|
|
|
key('ctrl+alt+v', function (e) {
|
|
|
|
changeMode(modeType.view)
|
|
|
|
})
|
|
|
|
key('ctrl+alt+b', function (e) {
|
|
|
|
changeMode(modeType.both)
|
|
|
|
})
|
2017-03-28 08:38:31 -04:00
|
|
|
// toggle-dropdown
|
2017-03-08 13:41:05 -05:00
|
|
|
$(document).on('click', '.toggle-dropdown .dropdown-menu', function (e) {
|
|
|
|
e.stopPropagation()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
// when page resize
|
2015-05-04 03:53:29 -04:00
|
|
|
$(window).resize(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
checkLayout()
|
|
|
|
checkEditorStyle()
|
|
|
|
checkTocStyle()
|
|
|
|
checkCursorMenu()
|
|
|
|
windowResize()
|
|
|
|
})
|
|
|
|
// when page unload
|
2016-07-30 00:19:42 -04:00
|
|
|
$(window).on('unload', function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
// updateHistoryInner();
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2016-07-30 00:19:42 -04:00
|
|
|
$(window).on('error', function () {
|
2017-03-28 08:38:31 -04:00
|
|
|
// setNeedRefresh();
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2016-09-18 04:51:19 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
setupSyncAreas(
|
|
|
|
ui.area.codemirrorScroll,
|
|
|
|
ui.area.view,
|
|
|
|
ui.area.markdown,
|
|
|
|
editor
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
function autoSyncscroll () {
|
|
|
|
if (editorHasFocus()) {
|
|
|
|
syncScrollToView()
|
|
|
|
} else {
|
|
|
|
syncScrollToEdit()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const windowResizeDebounce = 200
|
|
|
|
const windowResize = _.debounce(windowResizeInner, windowResizeDebounce)
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
function windowResizeInner (callback) {
|
|
|
|
checkLayout()
|
|
|
|
checkResponsive()
|
|
|
|
checkEditorStyle()
|
|
|
|
checkTocStyle()
|
|
|
|
checkCursorMenu()
|
2017-03-28 08:38:31 -04:00
|
|
|
// refresh editor
|
2017-03-08 13:41:05 -05:00
|
|
|
if (window.loaded) {
|
|
|
|
if (editor.getOption('scrollbarStyle') === 'native') {
|
|
|
|
setTimeout(function () {
|
|
|
|
clearMap()
|
|
|
|
autoSyncscroll()
|
|
|
|
updateScrollspy()
|
2021-02-15 03:42:51 -05:00
|
|
|
if (callback && typeof callback === 'function') {
|
|
|
|
callback()
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}, 1)
|
2016-05-29 01:58:32 -04:00
|
|
|
} else {
|
2017-03-28 08:38:31 -04:00
|
|
|
// force it load all docs at once to prevent scroll knob blink
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.setOption('viewportMargin', Infinity)
|
|
|
|
setTimeout(function () {
|
|
|
|
clearMap()
|
|
|
|
autoSyncscroll()
|
|
|
|
editor.setOption('viewportMargin', viewportMargin)
|
2017-03-28 08:38:31 -04:00
|
|
|
// add or update user cursors
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
|
|
|
if (onlineUsers[i].id !== personalInfo.id) {
|
|
|
|
buildCursor(onlineUsers[i])
|
|
|
|
}
|
2016-03-14 23:10:08 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
updateScrollspy()
|
2021-02-15 03:42:51 -05:00
|
|
|
if (callback && typeof callback === 'function') {
|
|
|
|
callback()
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}, 1)
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkLayout () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const navbarHieght = $('.navbar').outerHeight()
|
2017-03-08 13:41:05 -05:00
|
|
|
$('body').css('padding-top', navbarHieght + 'px')
|
2015-09-25 06:37:40 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function editorHasFocus () {
|
|
|
|
return $(editor.getInputField()).is(':focus')
|
2015-05-15 00:58:13 -04:00
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// 768-792px have a gap
|
|
|
|
function checkResponsive () {
|
2017-04-10 23:48:39 -04:00
|
|
|
visibleXS = $('.visible-xs').is(':visible')
|
|
|
|
visibleSM = $('.visible-sm').is(':visible')
|
|
|
|
visibleMD = $('.visible-md').is(':visible')
|
|
|
|
visibleLG = $('.visible-lg').is(':visible')
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-04-11 21:21:13 -04:00
|
|
|
if (visibleXS && appState.currentMode === modeType.both) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (editorHasFocus()) {
|
|
|
|
changeMode(modeType.edit)
|
|
|
|
} else {
|
|
|
|
changeMode(modeType.view)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
emitUserStatus()
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let lastEditorWidth = 0
|
|
|
|
let previousFocusOnEditor = null
|
2015-09-25 06:46:08 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkEditorStyle () {
|
2021-02-15 03:42:51 -05:00
|
|
|
let desireHeight = editorInstance.statusBar
|
|
|
|
? ui.area.edit.height() - editorInstance.statusBar.outerHeight()
|
|
|
|
: ui.area.edit.height()
|
2018-06-19 10:03:32 -04:00
|
|
|
if (editorInstance.toolBar) {
|
2018-06-23 14:55:32 -04:00
|
|
|
desireHeight = desireHeight - editorInstance.toolBar.outerHeight()
|
2018-06-19 10:03:32 -04:00
|
|
|
}
|
2018-06-23 14:55:32 -04:00
|
|
|
// set editor height and min height based on scrollbar style and mode
|
2021-02-15 03:42:51 -05:00
|
|
|
const scrollbarStyle = editor.getOption('scrollbarStyle')
|
2017-04-11 21:21:13 -04:00
|
|
|
if (scrollbarStyle === 'overlay' || appState.currentMode === modeType.both) {
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.area.codemirrorScroll.css('height', desireHeight + 'px')
|
|
|
|
ui.area.codemirrorScroll.css('min-height', '')
|
|
|
|
checkEditorScrollbar()
|
|
|
|
} else if (scrollbarStyle === 'native') {
|
|
|
|
ui.area.codemirrorScroll.css('height', '')
|
|
|
|
ui.area.codemirrorScroll.css('min-height', desireHeight + 'px')
|
|
|
|
}
|
2017-03-28 08:38:31 -04:00
|
|
|
// workaround editor will have wrong doc height when editor height changed
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.setSize(null, ui.area.edit.height())
|
2017-03-28 08:38:31 -04:00
|
|
|
// make editor resizable
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!ui.area.resize.handle.length) {
|
|
|
|
ui.area.edit.resizable({
|
|
|
|
handles: 'e',
|
|
|
|
maxWidth: $(window).width() * 0.7,
|
|
|
|
minWidth: $(window).width() * 0.2,
|
|
|
|
create: function (e, ui) {
|
2021-02-15 03:42:51 -05:00
|
|
|
$(this)
|
|
|
|
.parent()
|
|
|
|
.on('resize', function (e) {
|
|
|
|
e.stopPropagation()
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
},
|
|
|
|
start: function (e) {
|
|
|
|
editor.setOption('viewportMargin', Infinity)
|
|
|
|
},
|
|
|
|
resize: function (e) {
|
|
|
|
ui.area.resize.syncToggle.stop(true, true).show()
|
|
|
|
checkTocStyle()
|
|
|
|
},
|
|
|
|
stop: function (e) {
|
|
|
|
lastEditorWidth = ui.area.edit.width()
|
2017-03-22 05:48:26 -04:00
|
|
|
// workaround that scroll event bindings
|
2017-03-08 13:41:05 -05:00
|
|
|
window.preventSyncScrollToView = 2
|
|
|
|
window.preventSyncScrollToEdit = true
|
|
|
|
editor.setOption('viewportMargin', viewportMargin)
|
|
|
|
if (editorHasFocus()) {
|
|
|
|
windowResizeInner(function () {
|
|
|
|
ui.area.codemirrorScroll.scroll()
|
|
|
|
})
|
2016-05-26 12:12:07 -04:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
windowResizeInner(function () {
|
|
|
|
ui.area.view.scroll()
|
|
|
|
})
|
2016-05-26 12:12:07 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
checkEditorScrollbar()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
ui.area.resize.handle = $('.ui-resizable-handle')
|
|
|
|
}
|
|
|
|
if (!ui.area.resize.syncToggle.length) {
|
2021-02-15 03:42:51 -05:00
|
|
|
ui.area.resize.syncToggle = $(
|
|
|
|
'<button class="btn btn-lg btn-default ui-sync-toggle" title="Toggle sync scrolling"><i class="fa fa-link fa-fw"></i></button>'
|
|
|
|
)
|
|
|
|
ui.area.resize.syncToggle.hover(
|
|
|
|
function () {
|
|
|
|
previousFocusOnEditor = editorHasFocus()
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
previousFocusOnEditor = null
|
|
|
|
}
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.area.resize.syncToggle.click(function () {
|
2017-04-11 21:21:13 -04:00
|
|
|
appState.syncscroll = !appState.syncscroll
|
2017-03-08 13:41:05 -05:00
|
|
|
checkSyncToggle()
|
|
|
|
})
|
|
|
|
ui.area.resize.handle.append(ui.area.resize.syncToggle)
|
|
|
|
ui.area.resize.syncToggle.hide()
|
2021-02-15 03:42:51 -05:00
|
|
|
ui.area.resize.handle.hover(
|
|
|
|
function () {
|
|
|
|
ui.area.resize.syncToggle.stop(true, true).delay(200).fadeIn(100)
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
ui.area.resize.syncToggle.stop(true, true).delay(300).fadeOut(300)
|
|
|
|
}
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkSyncToggle () {
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.syncscroll) {
|
2017-03-08 13:41:05 -05:00
|
|
|
if (previousFocusOnEditor) {
|
|
|
|
window.preventSyncScrollToView = false
|
|
|
|
syncScrollToView()
|
2016-05-26 01:17:00 -04:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.preventSyncScrollToEdit = false
|
|
|
|
syncScrollToEdit()
|
2016-05-26 01:17:00 -04:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
ui.area.resize.syncToggle
|
|
|
|
.find('i')
|
|
|
|
.removeClass('fa-unlink')
|
|
|
|
.addClass('fa-link')
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
ui.area.resize.syncToggle
|
|
|
|
.find('i')
|
|
|
|
.removeClass('fa-link')
|
|
|
|
.addClass('fa-unlink')
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const checkEditorScrollbar = _.debounce(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.operation(checkEditorScrollbarInner)
|
|
|
|
}, 50)
|
2017-01-01 22:06:02 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkEditorScrollbarInner () {
|
2017-03-28 08:38:31 -04:00
|
|
|
// workaround simple scroll bar knob
|
|
|
|
// will get wrong position when editor height changed
|
2021-02-15 03:42:51 -05:00
|
|
|
const scrollInfo = editor.getScrollInfo()
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.scrollTo(null, scrollInfo.top - 1)
|
|
|
|
editor.scrollTo(null, scrollInfo.top)
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkTocStyle () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// toc right
|
2021-02-15 03:42:51 -05:00
|
|
|
const paddingRight = parseFloat(ui.area.markdown.css('padding-right'))
|
|
|
|
const right =
|
|
|
|
$(window).width() -
|
|
|
|
(ui.area.markdown.offset().left +
|
|
|
|
ui.area.markdown.outerWidth() -
|
|
|
|
paddingRight)
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toc.toc.css('right', right + 'px')
|
2019-05-30 18:27:56 -04:00
|
|
|
// affix toc left
|
2021-02-15 03:42:51 -05:00
|
|
|
let newbool
|
|
|
|
const rightMargin =
|
|
|
|
(ui.area.markdown.parent().outerWidth() - ui.area.markdown.outerWidth()) /
|
|
|
|
2
|
2019-05-30 18:27:56 -04:00
|
|
|
// for ipad or wider device
|
2017-03-08 13:41:05 -05:00
|
|
|
if (rightMargin >= 133) {
|
|
|
|
newbool = true
|
2021-02-15 03:42:51 -05:00
|
|
|
const affixLeftMargin =
|
|
|
|
(ui.toc.affix.outerWidth() - ui.toc.affix.width()) / 2
|
|
|
|
const left =
|
|
|
|
ui.area.markdown.offset().left +
|
|
|
|
ui.area.markdown.outerWidth() -
|
|
|
|
affixLeftMargin
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toc.affix.css('left', left + 'px')
|
|
|
|
ui.toc.affix.css('width', rightMargin + 'px')
|
|
|
|
} else {
|
|
|
|
newbool = false
|
|
|
|
}
|
2017-03-28 08:38:31 -04:00
|
|
|
// toc scrollspy
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toc.toc.removeClass('scrollspy-body, scrollspy-view')
|
|
|
|
ui.toc.affix.removeClass('scrollspy-body, scrollspy-view')
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode === modeType.both) {
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toc.toc.addClass('scrollspy-view')
|
|
|
|
ui.toc.affix.addClass('scrollspy-view')
|
2017-04-11 21:21:13 -04:00
|
|
|
} else if (appState.currentMode !== modeType.both && !newbool) {
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toc.toc.addClass('scrollspy-body')
|
|
|
|
ui.toc.affix.addClass('scrollspy-body')
|
|
|
|
} else {
|
|
|
|
ui.toc.toc.addClass('scrollspy-view')
|
|
|
|
ui.toc.affix.addClass('scrollspy-body')
|
|
|
|
}
|
|
|
|
if (newbool !== enoughForAffixToc) {
|
|
|
|
enoughForAffixToc = newbool
|
|
|
|
generateScrollspy()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showStatus (type, num) {
|
2017-04-11 00:07:04 -04:00
|
|
|
currentStatus = type
|
2021-02-15 03:42:51 -05:00
|
|
|
const shortStatus = ui.toolbar.shortStatus
|
|
|
|
const status = ui.toolbar.status
|
|
|
|
const label = $('<span class="label"></span>')
|
|
|
|
const fa = $('<i class="fa"></i>')
|
|
|
|
let msg = ''
|
|
|
|
let shortMsg = ''
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
shortStatus.html('')
|
|
|
|
status.html('')
|
|
|
|
|
2017-04-11 00:07:04 -04:00
|
|
|
switch (currentStatus) {
|
2017-03-08 13:41:05 -05:00
|
|
|
case statusType.connected:
|
|
|
|
label.addClass(statusType.connected.label)
|
|
|
|
fa.addClass(statusType.connected.fa)
|
|
|
|
msg = statusType.connected.msg
|
|
|
|
break
|
|
|
|
case statusType.online:
|
|
|
|
label.addClass(statusType.online.label)
|
|
|
|
fa.addClass(statusType.online.fa)
|
|
|
|
shortMsg = num
|
|
|
|
msg = num + ' ' + statusType.online.msg
|
|
|
|
break
|
|
|
|
case statusType.offline:
|
|
|
|
label.addClass(statusType.offline.label)
|
|
|
|
fa.addClass(statusType.offline.fa)
|
|
|
|
msg = statusType.offline.msg
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
label.append(fa)
|
2021-02-15 03:42:51 -05:00
|
|
|
const shortLabel = label.clone()
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
shortLabel.append(' ' + shortMsg)
|
|
|
|
shortStatus.append(shortLabel)
|
|
|
|
|
|
|
|
label.append(' ' + msg)
|
|
|
|
status.append(label)
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleMode () {
|
2017-04-11 21:21:13 -04:00
|
|
|
switch (appState.currentMode) {
|
2017-03-08 13:41:05 -05:00
|
|
|
case modeType.edit:
|
|
|
|
changeMode(modeType.view)
|
|
|
|
break
|
|
|
|
case modeType.view:
|
|
|
|
changeMode(modeType.edit)
|
|
|
|
break
|
|
|
|
case modeType.both:
|
|
|
|
changeMode(modeType.view)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let lastMode = null
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
function changeMode (type) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// lock navbar to prevent it hide after changeMode
|
2017-03-08 13:41:05 -05:00
|
|
|
lockNavbar()
|
|
|
|
saveInfo()
|
|
|
|
if (type) {
|
2017-04-11 21:21:13 -04:00
|
|
|
lastMode = appState.currentMode
|
|
|
|
appState.currentMode = type
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const responsiveClass = 'col-lg-6 col-md-6 col-sm-6'
|
|
|
|
const scrollClass = 'ui-scrollable'
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.area.codemirror.removeClass(scrollClass)
|
|
|
|
ui.area.edit.removeClass(responsiveClass)
|
|
|
|
ui.area.view.removeClass(scrollClass)
|
|
|
|
ui.area.view.removeClass(responsiveClass)
|
2017-04-11 21:21:13 -04:00
|
|
|
switch (appState.currentMode) {
|
2017-03-08 13:41:05 -05:00
|
|
|
case modeType.edit:
|
|
|
|
ui.area.edit.show()
|
|
|
|
ui.area.view.hide()
|
2017-04-10 23:48:39 -04:00
|
|
|
if (!editShown) {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.refresh()
|
2017-04-10 23:48:39 -04:00
|
|
|
editShown = true
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case modeType.view:
|
|
|
|
ui.area.edit.hide()
|
|
|
|
ui.area.view.show()
|
|
|
|
break
|
|
|
|
case modeType.both:
|
|
|
|
ui.area.codemirror.addClass(scrollClass)
|
|
|
|
ui.area.edit.addClass(responsiveClass).show()
|
|
|
|
ui.area.view.addClass(scrollClass)
|
|
|
|
ui.area.view.show()
|
|
|
|
break
|
|
|
|
}
|
2017-03-22 05:48:26 -04:00
|
|
|
// save mode to url
|
2021-02-15 03:42:51 -05:00
|
|
|
if (history.replaceState && window.loaded) {
|
|
|
|
history.replaceState(
|
|
|
|
null,
|
|
|
|
'',
|
|
|
|
serverurl + '/' + noteid + '?' + appState.currentMode.name
|
|
|
|
)
|
|
|
|
}
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode === modeType.view) {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.getInputField().blur()
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
appState.currentMode === modeType.edit ||
|
|
|
|
appState.currentMode === modeType.both
|
|
|
|
) {
|
2017-03-22 05:48:26 -04:00
|
|
|
// add and update status bar
|
2017-03-13 09:32:50 -04:00
|
|
|
if (!editorInstance.statusBar) {
|
|
|
|
editorInstance.addStatusBar()
|
2017-03-28 05:16:32 -04:00
|
|
|
editorInstance.updateStatusBar()
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2018-06-19 10:03:32 -04:00
|
|
|
// add and update tool bar
|
|
|
|
if (!editorInstance.toolBar) {
|
|
|
|
editorInstance.addToolBar()
|
|
|
|
}
|
2017-03-22 05:48:26 -04:00
|
|
|
// work around foldGutter might not init properly
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.setOption('foldGutter', false)
|
|
|
|
editor.setOption('foldGutter', true)
|
|
|
|
}
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode !== modeType.edit) {
|
2017-03-08 13:41:05 -05:00
|
|
|
$(document.body).css('background-color', 'white')
|
|
|
|
updateView()
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
$(document.body).css(
|
|
|
|
'background-color',
|
|
|
|
ui.area.codemirror.css('background-color')
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// check resizable editor style
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode === modeType.both) {
|
2017-03-13 09:32:50 -04:00
|
|
|
if (lastEditorWidth > 0) {
|
|
|
|
ui.area.edit.css('width', lastEditorWidth + 'px')
|
2015-09-25 06:46:08 -04:00
|
|
|
} else {
|
2017-03-13 09:32:50 -04:00
|
|
|
ui.area.edit.css('width', '')
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.area.resize.handle.show()
|
|
|
|
} else {
|
|
|
|
ui.area.edit.css('width', '')
|
|
|
|
ui.area.resize.handle.hide()
|
|
|
|
}
|
|
|
|
|
|
|
|
windowResizeInner()
|
|
|
|
|
|
|
|
restoreInfo()
|
|
|
|
|
2017-04-11 21:21:13 -04:00
|
|
|
if (lastMode === modeType.view && appState.currentMode === modeType.both) {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.preventSyncScrollToView = 2
|
|
|
|
syncScrollToEdit(null, true)
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:21:13 -04:00
|
|
|
if (lastMode === modeType.edit && appState.currentMode === modeType.both) {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.preventSyncScrollToEdit = 2
|
|
|
|
syncScrollToView(null, true)
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:21:13 -04:00
|
|
|
if (lastMode === modeType.both && appState.currentMode !== modeType.both) {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.preventSyncScrollToView = false
|
|
|
|
window.preventSyncScrollToEdit = false
|
|
|
|
}
|
|
|
|
|
2017-04-11 21:21:13 -04:00
|
|
|
if (lastMode !== modeType.edit && appState.currentMode === modeType.edit) {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.refresh()
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document.body).scrollspy('refresh')
|
|
|
|
ui.area.view.scrollspy('refresh')
|
|
|
|
|
|
|
|
ui.toolbar.both.removeClass('active')
|
|
|
|
ui.toolbar.edit.removeClass('active')
|
|
|
|
ui.toolbar.view.removeClass('active')
|
2021-02-15 03:42:51 -05:00
|
|
|
const modeIcon = ui.toolbar.mode.find('i')
|
2017-03-08 13:41:05 -05:00
|
|
|
modeIcon.removeClass('fa-pencil').removeClass('fa-eye')
|
2021-02-15 03:42:51 -05:00
|
|
|
if (ui.area.edit.is(':visible') && ui.area.view.is(':visible')) {
|
|
|
|
// both
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toolbar.both.addClass('active')
|
|
|
|
modeIcon.addClass('fa-eye')
|
2021-02-15 03:42:51 -05:00
|
|
|
} else if (ui.area.edit.is(':visible')) {
|
|
|
|
// edit
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toolbar.edit.addClass('active')
|
|
|
|
modeIcon.addClass('fa-eye')
|
2021-02-15 03:42:51 -05:00
|
|
|
} else if (ui.area.view.is(':visible')) {
|
|
|
|
// view
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toolbar.view.addClass('active')
|
|
|
|
modeIcon.addClass('fa-pencil')
|
|
|
|
}
|
|
|
|
unlockNavbar()
|
|
|
|
}
|
|
|
|
|
|
|
|
function lockNavbar () {
|
|
|
|
$('.navbar').addClass('locked')
|
2015-09-24 01:55:02 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const unlockNavbar = _.debounce(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
$('.navbar').removeClass('locked')
|
|
|
|
}, 200)
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function showMessageModal (title, header, href, text, success) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const modal = $('.message-modal')
|
2017-03-08 13:41:05 -05:00
|
|
|
modal.find('.modal-title').html(title)
|
|
|
|
modal.find('.modal-body h5').html(header)
|
2021-02-15 03:42:51 -05:00
|
|
|
if (href) {
|
|
|
|
modal.find('.modal-body a').attr('href', href).text(text)
|
|
|
|
} else {
|
|
|
|
modal.find('.modal-body a').removeAttr('href').text(text)
|
|
|
|
}
|
|
|
|
modal
|
|
|
|
.find('.modal-footer button')
|
|
|
|
.removeClass('btn-default btn-success btn-danger')
|
|
|
|
if (success) {
|
|
|
|
modal.find('.modal-footer button').addClass('btn-success')
|
|
|
|
} else {
|
|
|
|
modal.find('.modal-footer button').addClass('btn-danger')
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
modal.modal('show')
|
2016-03-04 10:17:35 -05:00
|
|
|
}
|
|
|
|
|
2016-05-14 22:54:24 -04:00
|
|
|
// check if dropbox app key is set and load scripts
|
|
|
|
if (DROPBOX_APP_KEY) {
|
2017-03-08 13:41:05 -05:00
|
|
|
$('<script>')
|
2019-05-30 18:27:56 -04:00
|
|
|
.attr('type', 'text/javascript')
|
|
|
|
.attr('src', 'https://www.dropbox.com/static/api/2/dropins.js')
|
|
|
|
.attr('id', 'dropboxjs')
|
|
|
|
.attr('data-app-key', DROPBOX_APP_KEY)
|
|
|
|
.prop('async', true)
|
|
|
|
.prop('defer', true)
|
|
|
|
.appendTo('body')
|
2016-05-14 22:54:24 -04:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.toolbar.import.dropbox.hide()
|
|
|
|
ui.toolbar.export.dropbox.hide()
|
2016-05-14 22:54:24 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// button actions
|
|
|
|
// share
|
|
|
|
ui.toolbar.publish.attr('href', noteurl + '/publish')
|
2016-07-02 04:18:10 -04:00
|
|
|
// extra
|
2017-03-08 13:41:05 -05:00
|
|
|
// slide
|
|
|
|
ui.toolbar.extra.slide.attr('href', noteurl + '/slide')
|
|
|
|
// download
|
|
|
|
// markdown
|
2015-12-18 10:44:08 -05:00
|
|
|
ui.toolbar.download.markdown.click(function (e) {
|
2017-03-08 13:41:05 -05:00
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
2021-02-15 03:42:51 -05:00
|
|
|
const filename = renderFilename(ui.area.markdown) + '.md'
|
|
|
|
const markdown = editor.getValue()
|
|
|
|
const blob = new Blob([markdown], {
|
2017-03-08 13:41:05 -05:00
|
|
|
type: 'text/markdown;charset=utf-8'
|
|
|
|
})
|
|
|
|
saveAs(blob, filename, true)
|
|
|
|
})
|
|
|
|
// html
|
2015-12-18 10:44:08 -05:00
|
|
|
ui.toolbar.download.html.click(function (e) {
|
2017-03-08 13:41:05 -05:00
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
exportToHTML(ui.area.markdown)
|
|
|
|
})
|
2016-06-17 04:17:37 -04:00
|
|
|
// raw html
|
|
|
|
ui.toolbar.download.rawhtml.click(function (e) {
|
2017-03-08 13:41:05 -05:00
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
exportToRawHTML(ui.area.markdown)
|
|
|
|
})
|
|
|
|
// export to dropbox
|
2020-08-22 19:21:37 -04:00
|
|
|
ui.toolbar.export.dropbox.click(function (event) {
|
|
|
|
event.preventDefault()
|
2021-02-15 03:42:51 -05:00
|
|
|
const filename = renderFilename(ui.area.markdown) + '.md'
|
|
|
|
const options = {
|
2017-03-08 13:41:05 -05:00
|
|
|
files: [
|
|
|
|
{
|
2021-02-15 03:42:51 -05:00
|
|
|
url: noteurl + '/download',
|
|
|
|
filename: filename
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
],
|
|
|
|
error: function (errorMessage) {
|
|
|
|
console.error(errorMessage)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Dropbox.save(options)
|
|
|
|
})
|
|
|
|
// export to gist
|
|
|
|
ui.toolbar.export.gist.attr('href', noteurl + '/gist')
|
|
|
|
// export to snippet
|
|
|
|
ui.toolbar.export.snippet.click(function () {
|
|
|
|
ui.spinner.show()
|
|
|
|
$.get(serverurl + '/auth/gitlab/callback/' + noteid + '/projects')
|
2019-05-30 18:27:56 -04:00
|
|
|
.done(function (data) {
|
|
|
|
$('#snippetExportModalAccessToken').val(data.accesstoken)
|
|
|
|
$('#snippetExportModalBaseURL').val(data.baseURL)
|
|
|
|
$('#snippetExportModalVersion').val(data.version)
|
|
|
|
$('#snippetExportModalLoading').hide()
|
|
|
|
$('#snippetExportModal').modal('toggle')
|
2021-02-15 03:42:51 -05:00
|
|
|
$('#snippetExportModalProjects')
|
|
|
|
.find('option')
|
|
|
|
.remove()
|
|
|
|
.end()
|
|
|
|
.append(
|
|
|
|
'<option value="init" selected="selected" disabled="disabled">Select From Available Projects</option>'
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
if (data.projects) {
|
|
|
|
data.projects.sort(function (a, b) {
|
2021-02-15 03:42:51 -05:00
|
|
|
return a.path_with_namespace < b.path_with_namespace
|
|
|
|
? -1
|
|
|
|
: a.path_with_namespace > b.path_with_namespace
|
|
|
|
? 1
|
|
|
|
: 0
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
data.projects.forEach(function (project) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
!project.snippets_enabled ||
|
|
|
|
(project.permissions.project_access === null &&
|
|
|
|
project.permissions.group_access === null) ||
|
|
|
|
(project.permissions.project_access !== null &&
|
|
|
|
project.permissions.project_access.access_level < 20)
|
|
|
|
) {
|
2019-05-30 18:27:56 -04:00
|
|
|
return
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
$('<option>')
|
|
|
|
.val(project.id)
|
|
|
|
.text(project.path_with_namespace)
|
|
|
|
.appendTo('#snippetExportModalProjects')
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
$('#snippetExportModalProjects').prop('disabled', false)
|
|
|
|
}
|
|
|
|
$('#snippetExportModalLoading').hide()
|
|
|
|
})
|
|
|
|
.fail(function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-gitlab"></i> Import from Snippet',
|
|
|
|
'Unable to fetch gitlab parameters :(',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
false
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
ui.spinner.hide()
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
// import from dropbox
|
2020-08-22 19:11:31 -04:00
|
|
|
ui.toolbar.import.dropbox.click(function (event) {
|
|
|
|
event.preventDefault()
|
2021-02-15 03:42:51 -05:00
|
|
|
const options = {
|
2017-03-08 13:41:05 -05:00
|
|
|
success: function (files) {
|
|
|
|
ui.spinner.show()
|
2021-02-15 03:42:51 -05:00
|
|
|
const url = files[0].link
|
2017-03-08 13:41:05 -05:00
|
|
|
importFromUrl(url)
|
|
|
|
},
|
|
|
|
linkType: 'direct',
|
|
|
|
multiselect: false,
|
|
|
|
extensions: ['.md', '.html']
|
|
|
|
}
|
|
|
|
Dropbox.choose(options)
|
|
|
|
})
|
|
|
|
// import from gist
|
2016-04-20 06:06:36 -04:00
|
|
|
ui.toolbar.import.gist.click(function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// na
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
// import from snippet
|
2016-05-09 22:38:13 -04:00
|
|
|
ui.toolbar.import.snippet.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.spinner.show()
|
|
|
|
$.get(serverurl + '/auth/gitlab/callback/' + noteid + '/projects')
|
2019-05-30 18:27:56 -04:00
|
|
|
.done(function (data) {
|
|
|
|
$('#snippetImportModalAccessToken').val(data.accesstoken)
|
|
|
|
$('#snippetImportModalBaseURL').val(data.baseURL)
|
|
|
|
$('#snippetImportModalVersion').val(data.version)
|
|
|
|
$('#snippetImportModalContent').prop('disabled', false)
|
|
|
|
$('#snippetImportModalConfirm').prop('disabled', false)
|
|
|
|
$('#snippetImportModalLoading').hide()
|
|
|
|
$('#snippetImportModal').modal('toggle')
|
2021-02-15 03:42:51 -05:00
|
|
|
$('#snippetImportModalProjects')
|
|
|
|
.find('option')
|
|
|
|
.remove()
|
|
|
|
.end()
|
|
|
|
.append(
|
|
|
|
'<option value="init" selected="selected" disabled="disabled">Select From Available Projects</option>'
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
if (data.projects) {
|
|
|
|
data.projects.sort(function (a, b) {
|
2021-02-15 03:42:51 -05:00
|
|
|
return a.path_with_namespace < b.path_with_namespace
|
|
|
|
? -1
|
|
|
|
: a.path_with_namespace > b.path_with_namespace
|
|
|
|
? 1
|
|
|
|
: 0
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
data.projects.forEach(function (project) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
!project.snippets_enabled ||
|
|
|
|
(project.permissions.project_access === null &&
|
|
|
|
project.permissions.group_access === null) ||
|
|
|
|
(project.permissions.project_access !== null &&
|
|
|
|
project.permissions.project_access.access_level < 20)
|
|
|
|
) {
|
2019-05-30 18:27:56 -04:00
|
|
|
return
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
$('<option>')
|
|
|
|
.val(project.id)
|
|
|
|
.text(project.path_with_namespace)
|
|
|
|
.appendTo('#snippetImportModalProjects')
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2019-05-30 18:27:56 -04:00
|
|
|
$('#snippetImportModalProjects').prop('disabled', false)
|
|
|
|
}
|
|
|
|
$('#snippetImportModalLoading').hide()
|
|
|
|
})
|
|
|
|
.fail(function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-gitlab"></i> Import from Snippet',
|
|
|
|
'Unable to fetch gitlab parameters :(',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
false
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
ui.spinner.hide()
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
// toc
|
2015-07-01 12:10:20 -04:00
|
|
|
ui.toc.dropdown.click(function (e) {
|
2017-03-08 13:41:05 -05:00
|
|
|
e.stopPropagation()
|
|
|
|
})
|
2017-01-15 23:42:21 -05:00
|
|
|
// prevent empty link change hash
|
|
|
|
$('a[href="#"]').click(function (e) {
|
2017-03-08 13:41:05 -05:00
|
|
|
e.preventDefault()
|
|
|
|
})
|
|
|
|
|
|
|
|
// modal actions
|
2021-02-15 03:42:51 -05:00
|
|
|
let revisions = []
|
|
|
|
let revisionViewer = null
|
|
|
|
let revisionInsert = []
|
|
|
|
let revisionDelete = []
|
|
|
|
let revisionInsertAnnotation = null
|
|
|
|
let revisionDeleteAnnotation = null
|
|
|
|
const revisionList = ui.modal.revision.find('.ui-revision-list')
|
|
|
|
let revision = null
|
|
|
|
let revisionTime = null
|
2016-06-17 04:15:53 -04:00
|
|
|
ui.modal.revision.on('show.bs.modal', function (e) {
|
2017-03-08 13:41:05 -05:00
|
|
|
$.get(noteurl + '/revision')
|
2019-05-30 18:27:56 -04:00
|
|
|
.done(function (data) {
|
|
|
|
parseRevisions(data.revision)
|
|
|
|
initRevisionViewer()
|
|
|
|
})
|
|
|
|
.fail(function (err) {
|
|
|
|
if (debug) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.debug(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
// na
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
function checkRevisionViewer () {
|
|
|
|
if (revisionViewer) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const container = $(revisionViewer.display.wrapper).parent()
|
2017-03-08 13:41:05 -05:00
|
|
|
$(revisionViewer.display.scroller).css('height', container.height() + 'px')
|
|
|
|
revisionViewer.refresh()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ui.modal.revision.on('shown.bs.modal', checkRevisionViewer)
|
|
|
|
$(window).resize(checkRevisionViewer)
|
|
|
|
function parseRevisions (_revisions) {
|
|
|
|
if (_revisions.length !== revisions) {
|
|
|
|
revisions = _revisions
|
2021-02-15 03:42:51 -05:00
|
|
|
let lastRevision = null
|
2017-03-08 13:41:05 -05:00
|
|
|
if (revisionList.children().length > 0) {
|
|
|
|
lastRevision = revisionList.find('.active').attr('data-revision-time')
|
|
|
|
}
|
|
|
|
revisionList.html('')
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < revisions.length; i++) {
|
|
|
|
const revision = revisions[i]
|
|
|
|
const item = $('<a class="list-group-item"></a>')
|
2017-03-08 13:41:05 -05:00
|
|
|
item.attr('data-revision-time', revision.time)
|
|
|
|
if (lastRevision === revision.time) item.addClass('active')
|
2021-02-15 03:42:51 -05:00
|
|
|
const itemHeading = $('<h5 class="list-group-item-heading"></h5>')
|
|
|
|
itemHeading.html(
|
|
|
|
'<i class="fa fa-clock-o"></i> ' + moment(revision.time).format('llll')
|
|
|
|
)
|
|
|
|
const itemText = $('<p class="list-group-item-text"></p>')
|
|
|
|
itemText.html(
|
|
|
|
'<i class="fa fa-file-text"></i> Length: ' + revision.length
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
item.append(itemHeading).append(itemText)
|
|
|
|
item.click(function (e) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const time = $(this).attr('data-revision-time')
|
2017-03-08 13:41:05 -05:00
|
|
|
selectRevision(time)
|
|
|
|
})
|
|
|
|
revisionList.append(item)
|
|
|
|
}
|
|
|
|
if (!lastRevision) {
|
|
|
|
selectRevision(revisions[0].time)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function selectRevision (time) {
|
|
|
|
if (time === revisionTime) return
|
|
|
|
$.get(noteurl + '/revision/' + time)
|
2019-05-30 18:27:56 -04:00
|
|
|
.done(function (data) {
|
|
|
|
revision = data
|
|
|
|
revisionTime = time
|
2021-02-15 03:42:51 -05:00
|
|
|
const lastScrollInfo = revisionViewer.getScrollInfo()
|
2019-05-30 18:27:56 -04:00
|
|
|
revisionList.children().removeClass('active')
|
2021-02-15 03:42:51 -05:00
|
|
|
revisionList
|
|
|
|
.find('[data-revision-time="' + time + '"]')
|
|
|
|
.addClass('active')
|
|
|
|
const content = revision.content
|
2019-05-30 18:27:56 -04:00
|
|
|
revisionViewer.setValue(content)
|
|
|
|
revisionViewer.scrollTo(null, lastScrollInfo.top)
|
|
|
|
revisionInsert = []
|
|
|
|
revisionDelete = []
|
|
|
|
// mark the text which have been insert or delete
|
|
|
|
if (revision.patch.length > 0) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let bias = 0
|
|
|
|
for (let j = 0; j < revision.patch.length; j++) {
|
|
|
|
const patch = revision.patch[j]
|
|
|
|
let currIndex = patch.start1 + bias
|
|
|
|
for (let i = 0; i < patch.diffs.length; i++) {
|
|
|
|
const diff = patch.diffs[i]
|
2019-05-30 18:27:56 -04:00
|
|
|
// ignore if diff only contains line breaks
|
2021-02-15 03:42:51 -05:00
|
|
|
if ((diff[1].match(/\n/g) || []).length === diff[1].length) { continue }
|
|
|
|
let prePos, postPos
|
2019-05-30 18:27:56 -04:00
|
|
|
switch (diff[0]) {
|
|
|
|
case 0: // retain
|
|
|
|
currIndex += diff[1].length
|
|
|
|
break
|
|
|
|
case 1: // insert
|
|
|
|
prePos = revisionViewer.posFromIndex(currIndex)
|
2021-02-15 03:42:51 -05:00
|
|
|
postPos = revisionViewer.posFromIndex(
|
|
|
|
currIndex + diff[1].length
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
revisionInsert.push({
|
|
|
|
from: prePos,
|
|
|
|
to: postPos
|
|
|
|
})
|
|
|
|
revisionViewer.markText(prePos, postPos, {
|
2021-02-15 03:42:51 -05:00
|
|
|
css:
|
|
|
|
'background-color: rgba(230,255,230,0.7); text-decoration: underline;'
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
currIndex += diff[1].length
|
|
|
|
break
|
|
|
|
case -1: // delete
|
|
|
|
prePos = revisionViewer.posFromIndex(currIndex)
|
|
|
|
revisionViewer.replaceRange(diff[1], prePos)
|
2021-02-15 03:42:51 -05:00
|
|
|
postPos = revisionViewer.posFromIndex(
|
|
|
|
currIndex + diff[1].length
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
revisionDelete.push({
|
|
|
|
from: prePos,
|
|
|
|
to: postPos
|
|
|
|
})
|
|
|
|
revisionViewer.markText(prePos, postPos, {
|
2021-02-15 03:42:51 -05:00
|
|
|
css:
|
|
|
|
'background-color: rgba(255,230,230,0.7); text-decoration: line-through;'
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
bias += diff[1].length
|
|
|
|
currIndex += diff[1].length
|
|
|
|
break
|
2016-06-17 04:15:53 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
revisionInsertAnnotation.update(revisionInsert)
|
|
|
|
revisionDeleteAnnotation.update(revisionDelete)
|
|
|
|
})
|
|
|
|
.fail(function (err) {
|
|
|
|
if (debug) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.debug(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
// na
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
function initRevisionViewer () {
|
|
|
|
if (revisionViewer) return
|
2021-02-15 03:42:51 -05:00
|
|
|
const revisionViewerTextArea = document.getElementById('revisionViewer')
|
2017-03-08 13:41:05 -05:00
|
|
|
revisionViewer = CodeMirror.fromTextArea(revisionViewerTextArea, {
|
|
|
|
mode: defaultEditorMode,
|
|
|
|
viewportMargin: viewportMargin,
|
|
|
|
lineNumbers: true,
|
|
|
|
lineWrapping: true,
|
|
|
|
showCursorWhenSelecting: true,
|
|
|
|
inputStyle: 'textarea',
|
|
|
|
gutters: ['CodeMirror-linenumbers'],
|
|
|
|
flattenSpans: true,
|
|
|
|
addModeClass: true,
|
|
|
|
readOnly: true,
|
|
|
|
autoRefresh: true,
|
|
|
|
scrollbarStyle: 'overlay'
|
|
|
|
})
|
2021-02-15 03:42:51 -05:00
|
|
|
revisionInsertAnnotation = revisionViewer.annotateScrollbar({
|
|
|
|
className: 'CodeMirror-insert-match'
|
|
|
|
})
|
|
|
|
revisionDeleteAnnotation = revisionViewer.annotateScrollbar({
|
|
|
|
className: 'CodeMirror-delete-match'
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
checkRevisionViewer()
|
2016-06-17 04:15:53 -04:00
|
|
|
}
|
|
|
|
$('#revisionModalDownload').click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!revision) return
|
2021-02-15 03:42:51 -05:00
|
|
|
const filename =
|
|
|
|
renderFilename(ui.area.markdown) + '_' + revisionTime + '.md'
|
|
|
|
const blob = new Blob([revision.content], {
|
2017-03-08 13:41:05 -05:00
|
|
|
type: 'text/markdown;charset=utf-8'
|
|
|
|
})
|
|
|
|
saveAs(blob, filename, true)
|
|
|
|
})
|
2016-06-17 04:15:53 -04:00
|
|
|
$('#revisionModalRevert').click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!revision) return
|
|
|
|
editor.setValue(revision.content)
|
|
|
|
ui.modal.revision.modal('hide')
|
|
|
|
})
|
|
|
|
// snippet projects
|
|
|
|
ui.modal.snippetImportProjects.change(function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const accesstoken = $('#snippetImportModalAccessToken').val()
|
|
|
|
const baseURL = $('#snippetImportModalBaseURL').val()
|
|
|
|
const project = $('#snippetImportModalProjects').val()
|
|
|
|
const version = $('#snippetImportModalVersion').val()
|
2017-03-08 13:41:05 -05:00
|
|
|
$('#snippetImportModalLoading').show()
|
|
|
|
$('#snippetImportModalContent').val('/projects/' + project)
|
2021-02-15 03:42:51 -05:00
|
|
|
$.get(
|
|
|
|
baseURL +
|
|
|
|
'/api/' +
|
|
|
|
version +
|
|
|
|
'/projects/' +
|
|
|
|
project +
|
|
|
|
'/snippets?access_token=' +
|
|
|
|
accesstoken
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
.done(function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
$('#snippetImportModalSnippets')
|
|
|
|
.find('option')
|
|
|
|
.remove()
|
|
|
|
.end()
|
|
|
|
.append(
|
|
|
|
'<option value="init" selected="selected" disabled="disabled">Select From Available Snippets</option>'
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
data.forEach(function (snippet) {
|
2021-02-15 03:42:51 -05:00
|
|
|
$('<option>')
|
|
|
|
.val(snippet.id)
|
|
|
|
.text(snippet.title)
|
|
|
|
.appendTo($('#snippetImportModalSnippets'))
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
$('#snippetImportModalLoading').hide()
|
|
|
|
$('#snippetImportModalSnippets').prop('disabled', false)
|
|
|
|
})
|
|
|
|
.fail(function (err) {
|
|
|
|
if (debug) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.debug(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
// na
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
// snippet snippets
|
|
|
|
ui.modal.snippetImportSnippets.change(function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const snippet = $('#snippetImportModalSnippets').val()
|
|
|
|
$('#snippetImportModalContent').val(
|
|
|
|
$('#snippetImportModalContent').val() + '/snippets/' + snippet
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2015-09-25 06:29:01 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function scrollToTop () {
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode === modeType.both) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (editor.getScrollInfo().top !== 0) {
|
|
|
|
editor.scrollTo(0, 0)
|
|
|
|
} else {
|
|
|
|
ui.area.view.animate(
|
|
|
|
{
|
|
|
|
scrollTop: 0
|
|
|
|
},
|
|
|
|
100,
|
|
|
|
'linear'
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
$('body, html').stop(true, true).animate(
|
|
|
|
{
|
|
|
|
scrollTop: 0
|
|
|
|
},
|
|
|
|
100,
|
|
|
|
'linear'
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function scrollToBottom () {
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode === modeType.both) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const scrollInfo = editor.getScrollInfo()
|
|
|
|
const scrollHeight = scrollInfo.height
|
|
|
|
if (scrollInfo.top !== scrollHeight) {
|
|
|
|
editor.scrollTo(0, scrollHeight * 2)
|
|
|
|
} else {
|
|
|
|
ui.area.view.animate(
|
|
|
|
{
|
|
|
|
scrollTop: ui.area.view[0].scrollHeight
|
|
|
|
},
|
|
|
|
100,
|
|
|
|
'linear'
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
$('body, html')
|
|
|
|
.stop(true, true)
|
|
|
|
.animate(
|
|
|
|
{
|
|
|
|
scrollTop: $(document.body)[0].scrollHeight
|
|
|
|
},
|
|
|
|
100,
|
|
|
|
'linear'
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.scrollToTop = scrollToTop
|
|
|
|
window.scrollToBottom = scrollToBottom
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let enoughForAffixToc = true
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
// scrollspy
|
|
|
|
function generateScrollspy () {
|
|
|
|
$(document.body).scrollspy({
|
|
|
|
target: '.scrollspy-body'
|
|
|
|
})
|
|
|
|
ui.area.view.scrollspy({
|
|
|
|
target: '.scrollspy-view'
|
|
|
|
})
|
|
|
|
$(document.body).scrollspy('refresh')
|
|
|
|
ui.area.view.scrollspy('refresh')
|
|
|
|
if (enoughForAffixToc) {
|
|
|
|
ui.toc.toc.hide()
|
|
|
|
ui.toc.affix.show()
|
|
|
|
} else {
|
|
|
|
ui.toc.affix.hide()
|
|
|
|
ui.toc.toc.show()
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// $(document.body).scroll();
|
|
|
|
// ui.area.view.scroll();
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function updateScrollspy () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const headers = ui.area.markdown.find('h1, h2, h3').toArray()
|
|
|
|
const headerMap = []
|
|
|
|
for (let i = 0; i < headers.length; i++) {
|
|
|
|
headerMap.push(
|
|
|
|
$(headers[i]).offset().top - parseInt($(headers[i]).css('margin-top'))
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
applyScrollspyActive(
|
|
|
|
$(window).scrollTop(),
|
|
|
|
headerMap,
|
|
|
|
headers,
|
|
|
|
$('.scrollspy-body'),
|
|
|
|
0
|
|
|
|
)
|
|
|
|
const offset = ui.area.view.scrollTop() - ui.area.view.offset().top
|
|
|
|
applyScrollspyActive(
|
|
|
|
ui.area.view.scrollTop(),
|
|
|
|
headerMap,
|
|
|
|
headers,
|
|
|
|
$('.scrollspy-view'),
|
|
|
|
offset - 10
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function applyScrollspyActive (top, headerMap, headers, target, offset) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let index = 0
|
|
|
|
for (let i = headerMap.length - 1; i >= 0; i--) {
|
|
|
|
if (
|
|
|
|
top >= headerMap[i] + offset &&
|
|
|
|
headerMap[i + 1] &&
|
|
|
|
top < headerMap[i + 1] + offset
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
index = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const header = $(headers[index])
|
|
|
|
const active = target.find('a[href="#' + header.attr('id') + '"]')
|
|
|
|
active
|
|
|
|
.closest('li')
|
|
|
|
.addClass('active')
|
|
|
|
.parent()
|
|
|
|
.closest('li')
|
|
|
|
.addClass('active')
|
|
|
|
.parent()
|
|
|
|
.closest('li')
|
|
|
|
.addClass('active')
|
2015-09-25 06:29:01 -04:00
|
|
|
}
|
|
|
|
|
2016-04-20 06:06:36 -04:00
|
|
|
// clipboard modal
|
2017-03-08 13:41:05 -05:00
|
|
|
// fix for wrong autofocus
|
2015-05-15 00:58:13 -04:00
|
|
|
$('#clipboardModal').on('shown.bs.modal', function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
$('#clipboardModal').blur()
|
|
|
|
})
|
|
|
|
$('#clipboardModalClear').click(function () {
|
|
|
|
$('#clipboardModalContent').html('')
|
|
|
|
})
|
|
|
|
$('#clipboardModalConfirm').click(function () {
|
2021-02-13 11:54:38 -05:00
|
|
|
const data = $('#clipboardModalContent').text()
|
2017-03-08 13:41:05 -05:00
|
|
|
if (data) {
|
|
|
|
parseToEditor(data)
|
|
|
|
$('#clipboardModal').modal('hide')
|
|
|
|
$('#clipboardModalContent').html('')
|
|
|
|
}
|
|
|
|
})
|
2016-04-20 06:06:36 -04:00
|
|
|
|
|
|
|
// refresh modal
|
2015-07-01 12:10:20 -04:00
|
|
|
$('#refreshModalRefresh').click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
location.reload(true)
|
|
|
|
})
|
2015-05-15 00:58:13 -04:00
|
|
|
|
2016-04-20 06:06:36 -04:00
|
|
|
// gist import modal
|
2017-03-08 13:41:05 -05:00
|
|
|
$('#gistImportModalClear').click(function () {
|
|
|
|
$('#gistImportModalContent').val('')
|
|
|
|
})
|
|
|
|
$('#gistImportModalConfirm').click(function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const gisturl = $('#gistImportModalContent').val()
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!gisturl) return
|
|
|
|
$('#gistImportModal').modal('hide')
|
|
|
|
$('#gistImportModalContent').val('')
|
|
|
|
if (!isValidURL(gisturl)) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-github"></i> Import from Gist',
|
|
|
|
'Not a valid URL :(',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
false
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
const hostname = url('hostname', gisturl)
|
2017-03-08 13:41:05 -05:00
|
|
|
if (hostname !== 'gist.github.com') {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-github"></i> Import from Gist',
|
|
|
|
'Not a valid Gist URL :(',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
false
|
|
|
|
)
|
2016-04-20 06:06:36 -04:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.spinner.show()
|
2019-04-16 13:27:02 -04:00
|
|
|
$.get('https://api.github.com/gists/' + url('-1', gisturl))
|
2019-05-30 18:27:56 -04:00
|
|
|
.done(function (data) {
|
|
|
|
if (data.files) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let contents = ''
|
2019-05-30 18:27:56 -04:00
|
|
|
Object.keys(data.files).forEach(function (key) {
|
|
|
|
contents += key
|
|
|
|
contents += '\n---\n'
|
|
|
|
contents += data.files[key].content
|
|
|
|
contents += '\n\n'
|
|
|
|
})
|
|
|
|
replaceAll(contents)
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-github"></i> Import from Gist',
|
|
|
|
'Unable to fetch gist files :(',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
false
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.fail(function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-github"></i> Import from Gist',
|
|
|
|
'Not a valid Gist URL :(',
|
|
|
|
'',
|
|
|
|
JSON.stringify(data),
|
|
|
|
false
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
ui.spinner.hide()
|
|
|
|
})
|
2016-04-20 06:06:36 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
})
|
2016-04-20 06:06:36 -04:00
|
|
|
|
2016-05-09 22:38:13 -04:00
|
|
|
// snippet import modal
|
2017-03-08 13:41:05 -05:00
|
|
|
$('#snippetImportModalClear').click(function () {
|
|
|
|
$('#snippetImportModalContent').val('')
|
|
|
|
$('#snippetImportModalProjects').val('init')
|
|
|
|
$('#snippetImportModalSnippets').val('init')
|
|
|
|
$('#snippetImportModalSnippets').prop('disabled', true)
|
|
|
|
})
|
|
|
|
$('#snippetImportModalConfirm').click(function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const snippeturl = $('#snippetImportModalContent').val()
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!snippeturl) return
|
|
|
|
$('#snippetImportModal').modal('hide')
|
|
|
|
$('#snippetImportModalContent').val('')
|
|
|
|
if (!/^.+\/snippets\/.+$/.test(snippeturl)) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-github"></i> Import from Snippet',
|
|
|
|
'Not a valid Snippet URL :(',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
false
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
|
|
|
ui.spinner.show()
|
2021-02-15 03:42:51 -05:00
|
|
|
const accessToken =
|
|
|
|
'?access_token=' + $('#snippetImportModalAccessToken').val()
|
|
|
|
const fullURL =
|
|
|
|
$('#snippetImportModalBaseURL').val() +
|
|
|
|
'/api/' +
|
|
|
|
$('#snippetImportModalVersion').val() +
|
|
|
|
snippeturl
|
2017-03-08 13:41:05 -05:00
|
|
|
$.get(fullURL + accessToken)
|
2019-05-30 18:27:56 -04:00
|
|
|
.done(function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let content = '# ' + (data.title || 'Snippet Import')
|
|
|
|
const fileInfo = data.file_name.split('.')
|
|
|
|
fileInfo[1] = fileInfo[1] ? fileInfo[1] : 'md'
|
2019-05-30 18:27:56 -04:00
|
|
|
$.get(fullURL + '/raw' + accessToken)
|
|
|
|
.done(function (raw) {
|
|
|
|
if (raw) {
|
|
|
|
content += '\n\n'
|
|
|
|
if (fileInfo[1] !== 'md') {
|
|
|
|
content += '```' + fileTypes[fileInfo[1]] + '\n'
|
|
|
|
}
|
|
|
|
content += raw
|
|
|
|
if (fileInfo[1] !== 'md') {
|
|
|
|
content += '\n```'
|
|
|
|
}
|
|
|
|
replaceAll(content)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.fail(function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-gitlab"></i> Import from Snippet',
|
|
|
|
'Not a valid Snippet URL :(',
|
|
|
|
'',
|
|
|
|
JSON.stringify(data),
|
|
|
|
false
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
|
|
|
.always(function () {
|
|
|
|
ui.spinner.hide()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.fail(function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-gitlab"></i> Import from Snippet',
|
|
|
|
'Not a valid Snippet URL :(',
|
|
|
|
'',
|
|
|
|
JSON.stringify(data),
|
|
|
|
false
|
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// snippet export modal
|
|
|
|
$('#snippetExportModalConfirm').click(function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const accesstoken = $('#snippetExportModalAccessToken').val()
|
|
|
|
const baseURL = $('#snippetExportModalBaseURL').val()
|
|
|
|
const version = $('#snippetExportModalVersion').val()
|
2018-07-30 09:47:09 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const data = {
|
2017-03-08 13:41:05 -05:00
|
|
|
title: $('#snippetExportModalTitle').val(),
|
|
|
|
file_name: $('#snippetExportModalFileName').val(),
|
|
|
|
code: editor.getValue(),
|
2018-07-30 09:47:09 -04:00
|
|
|
visibility_level: $('#snippetExportModalVisibility').val(),
|
2021-02-15 03:42:51 -05:00
|
|
|
visibility:
|
|
|
|
$('#snippetExportModalVisibility').val() === '0'
|
|
|
|
? 'private'
|
|
|
|
: $('#snippetExportModalVisibility').val() === '10'
|
|
|
|
? 'internal'
|
|
|
|
: 'private'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
!data.title ||
|
|
|
|
!data.file_name ||
|
|
|
|
!data.code ||
|
|
|
|
!data.visibility_level ||
|
|
|
|
!$('#snippetExportModalProjects').val()
|
|
|
|
) { return }
|
2017-03-08 13:41:05 -05:00
|
|
|
$('#snippetExportModalLoading').show()
|
2021-02-15 03:42:51 -05:00
|
|
|
const fullURL =
|
|
|
|
baseURL +
|
|
|
|
'/api/' +
|
|
|
|
version +
|
|
|
|
'/projects/' +
|
|
|
|
$('#snippetExportModalProjects').val() +
|
|
|
|
'/snippets?access_token=' +
|
|
|
|
accesstoken
|
|
|
|
$.post(fullURL, data, function (ret) {
|
|
|
|
$('#snippetExportModalLoading').hide()
|
|
|
|
$('#snippetExportModal').modal('hide')
|
|
|
|
const redirect =
|
|
|
|
baseURL +
|
|
|
|
'/' +
|
|
|
|
$(
|
|
|
|
"#snippetExportModalProjects option[value='" +
|
|
|
|
$('#snippetExportModalProjects').val() +
|
|
|
|
"']"
|
|
|
|
).text() +
|
|
|
|
'/snippets/' +
|
|
|
|
ret.id
|
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-gitlab"></i> Export to Snippet',
|
|
|
|
'Export Successful!',
|
|
|
|
redirect,
|
|
|
|
'View Snippet Here',
|
|
|
|
true
|
|
|
|
)
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
function parseToEditor (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const turndownService = new TurndownService({
|
2018-11-21 05:11:47 -05:00
|
|
|
defaultReplacement: function (innerHTML, node) {
|
|
|
|
return node.isBlock ? '\n\n' + node.outerHTML + '\n\n' : node.outerHTML
|
|
|
|
}
|
|
|
|
})
|
2021-02-15 03:42:51 -05:00
|
|
|
const parsed = turndownService.turndown(data)
|
|
|
|
if (parsed) {
|
|
|
|
replaceAll(parsed)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function replaceAll (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
editor.replaceRange(
|
|
|
|
data,
|
|
|
|
{
|
|
|
|
line: 0,
|
|
|
|
ch: 0
|
|
|
|
},
|
|
|
|
{
|
|
|
|
line: editor.lastLine(),
|
|
|
|
ch: editor.lastLine().length
|
|
|
|
},
|
|
|
|
'+input'
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function importFromUrl (url) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// console.debug(url);
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!url) return
|
|
|
|
if (!isValidURL(url)) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-cloud-download"></i> Import from URL',
|
|
|
|
'Not a valid URL :(',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
false
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
$.ajax({
|
|
|
|
method: 'GET',
|
|
|
|
url: url,
|
|
|
|
success: function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const extension = url.split('.').pop()
|
|
|
|
if (extension === 'html') {
|
|
|
|
parseToEditor(data)
|
|
|
|
} else {
|
|
|
|
replaceAll(data)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
},
|
|
|
|
error: function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
showMessageModal(
|
|
|
|
'<i class="fa fa-cloud-download"></i> Import from URL',
|
|
|
|
'Import failed :(',
|
|
|
|
'',
|
|
|
|
JSON.stringify(data),
|
|
|
|
false
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
},
|
|
|
|
complete: function () {
|
|
|
|
ui.spinner.hide()
|
|
|
|
}
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2015-05-15 00:58:13 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// mode
|
2015-05-04 03:53:29 -04:00
|
|
|
ui.toolbar.mode.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
toggleMode()
|
|
|
|
})
|
|
|
|
// edit
|
2015-05-04 03:53:29 -04:00
|
|
|
ui.toolbar.edit.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
changeMode(modeType.edit)
|
|
|
|
})
|
|
|
|
// view
|
2015-05-04 03:53:29 -04:00
|
|
|
ui.toolbar.view.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
changeMode(modeType.view)
|
|
|
|
})
|
|
|
|
// both
|
2015-05-04 03:53:29 -04:00
|
|
|
ui.toolbar.both.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
changeMode(modeType.both)
|
|
|
|
})
|
2017-12-28 13:21:52 -05:00
|
|
|
|
|
|
|
ui.toolbar.night.click(function () {
|
|
|
|
toggleNightMode()
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
// permission
|
|
|
|
// freely
|
2015-07-01 12:10:20 -04:00
|
|
|
ui.infobar.permission.freely.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
emitPermission('freely')
|
|
|
|
})
|
|
|
|
// editable
|
2015-07-01 12:10:20 -04:00
|
|
|
ui.infobar.permission.editable.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
emitPermission('editable')
|
|
|
|
})
|
|
|
|
// locked
|
2015-07-01 12:10:20 -04:00
|
|
|
ui.infobar.permission.locked.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
emitPermission('locked')
|
|
|
|
})
|
|
|
|
// private
|
2016-01-17 10:51:27 -05:00
|
|
|
ui.infobar.permission.private.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
emitPermission('private')
|
|
|
|
})
|
|
|
|
// limited
|
|
|
|
ui.infobar.permission.limited.click(function () {
|
|
|
|
emitPermission('limited')
|
|
|
|
})
|
|
|
|
// protected
|
|
|
|
ui.infobar.permission.protected.click(function () {
|
|
|
|
emitPermission('protected')
|
|
|
|
})
|
2016-10-10 09:04:24 -04:00
|
|
|
// delete note
|
|
|
|
ui.infobar.delete.click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
$('.delete-modal').modal('show')
|
|
|
|
})
|
2016-10-10 09:04:24 -04:00
|
|
|
$('.ui-delete-modal-confirm').click(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
socket.emit('delete')
|
|
|
|
})
|
|
|
|
|
2017-12-28 13:21:52 -05:00
|
|
|
function toggleNightMode () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const $body = $('body')
|
|
|
|
const isActive = ui.toolbar.night.hasClass('active')
|
2017-12-28 13:21:52 -05:00
|
|
|
if (isActive) {
|
|
|
|
$body.removeClass('night')
|
|
|
|
appState.nightMode = false
|
|
|
|
} else {
|
|
|
|
$body.addClass('night')
|
|
|
|
appState.nightMode = true
|
|
|
|
}
|
2018-03-23 10:39:16 -04:00
|
|
|
if (store.enabled) {
|
|
|
|
store.set('nightMode', !isActive)
|
|
|
|
} else {
|
|
|
|
Cookies.set('nightMode', !isActive, {
|
2020-06-08 09:27:31 -04:00
|
|
|
expires: 365,
|
2020-08-26 20:04:49 -04:00
|
|
|
sameSite: window.cookiePolicy
|
2018-03-23 10:39:16 -04:00
|
|
|
})
|
|
|
|
}
|
2017-12-28 13:21:52 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
function emitPermission (_permission) {
|
|
|
|
if (_permission !== permission) {
|
|
|
|
socket.emit('permission', _permission)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function updatePermission (newPermission) {
|
|
|
|
if (permission !== newPermission) {
|
|
|
|
permission = newPermission
|
|
|
|
if (window.loaded) refreshView()
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
let label = null
|
|
|
|
let title = null
|
2017-03-08 13:41:05 -05:00
|
|
|
switch (permission) {
|
|
|
|
case 'freely':
|
|
|
|
label = '<i class="fa fa-leaf"></i> Freely'
|
|
|
|
title = 'Anyone can edit'
|
|
|
|
break
|
|
|
|
case 'editable':
|
|
|
|
label = '<i class="fa fa-shield"></i> Editable'
|
|
|
|
title = 'Signed people can edit'
|
|
|
|
break
|
|
|
|
case 'limited':
|
|
|
|
label = '<i class="fa fa-id-card"></i> Limited'
|
|
|
|
title = 'Signed people can edit (forbid guest)'
|
|
|
|
break
|
|
|
|
case 'locked':
|
|
|
|
label = '<i class="fa fa-lock"></i> Locked'
|
|
|
|
title = 'Only owner can edit'
|
|
|
|
break
|
|
|
|
case 'protected':
|
|
|
|
label = '<i class="fa fa-umbrella"></i> Protected'
|
|
|
|
title = 'Only owner can edit (forbid guest)'
|
|
|
|
break
|
|
|
|
case 'private':
|
|
|
|
label = '<i class="fa fa-hand-stop-o"></i> Private'
|
|
|
|
title = 'Only owner can view & edit'
|
|
|
|
break
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
personalInfo.userid &&
|
|
|
|
window.owner &&
|
|
|
|
personalInfo.userid === window.owner
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
label += ' <i class="fa fa-caret-down"></i>'
|
|
|
|
ui.infobar.permission.label.removeClass('disabled')
|
|
|
|
} else {
|
|
|
|
ui.infobar.permission.label.addClass('disabled')
|
|
|
|
}
|
|
|
|
ui.infobar.permission.label.html(label).attr('title', title)
|
|
|
|
}
|
|
|
|
|
|
|
|
function havePermission () {
|
2021-02-15 03:42:51 -05:00
|
|
|
let bool = false
|
2017-03-08 13:41:05 -05:00
|
|
|
switch (permission) {
|
|
|
|
case 'freely':
|
|
|
|
bool = true
|
|
|
|
break
|
|
|
|
case 'editable':
|
|
|
|
case 'limited':
|
2017-04-11 00:07:04 -04:00
|
|
|
if (!personalInfo.login) {
|
2017-03-08 13:41:05 -05:00
|
|
|
bool = false
|
|
|
|
} else {
|
|
|
|
bool = true
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'locked':
|
|
|
|
case 'private':
|
|
|
|
case 'protected':
|
2017-04-11 00:07:04 -04:00
|
|
|
if (!window.owner || personalInfo.userid !== window.owner) {
|
2017-03-08 13:41:05 -05:00
|
|
|
bool = false
|
|
|
|
} else {
|
|
|
|
bool = true
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return bool
|
2015-09-25 02:02:34 -04:00
|
|
|
}
|
2016-10-08 22:51:39 -04:00
|
|
|
// global module workaround
|
2017-03-08 13:41:05 -05:00
|
|
|
window.havePermission = havePermission
|
2015-09-25 02:02:34 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// socket.io actions
|
2021-02-15 03:42:51 -05:00
|
|
|
const io = require('socket.io-client')
|
|
|
|
const socket = io.connect({
|
2017-03-08 13:41:05 -05:00
|
|
|
path: urlpath ? '/' + urlpath + '/socket.io/' : '',
|
2017-09-13 08:48:39 -04:00
|
|
|
query: {
|
|
|
|
noteId: noteid
|
|
|
|
},
|
2017-03-08 13:41:05 -05:00
|
|
|
timeout: 5000, // 5 secs to timeout,
|
|
|
|
reconnectionAttempts: 20 // retry 20 times on connect failed
|
|
|
|
})
|
|
|
|
// overwrite original event for checking login state
|
2021-02-15 03:42:51 -05:00
|
|
|
const on = socket.on
|
2015-06-01 06:04:25 -04:00
|
|
|
socket.on = function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (!checkLoginStateChanged() && !needRefresh) {
|
|
|
|
return on.apply(socket, arguments)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const emit = socket.emit
|
2015-06-01 06:04:25 -04:00
|
|
|
socket.emit = function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (!checkLoginStateChanged() && !needRefresh) {
|
|
|
|
emit.apply(socket, arguments)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('info', function (data) {
|
2017-03-08 13:41:05 -05:00
|
|
|
console.error(data)
|
|
|
|
switch (data.code) {
|
|
|
|
case 403:
|
|
|
|
location.href = serverurl + '/403'
|
|
|
|
break
|
|
|
|
case 404:
|
|
|
|
location.href = serverurl + '/404'
|
|
|
|
break
|
|
|
|
case 500:
|
|
|
|
location.href = serverurl + '/500'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
2015-12-30 00:33:36 -05:00
|
|
|
socket.on('error', function (data) {
|
2017-03-08 13:41:05 -05:00
|
|
|
console.error(data)
|
2021-02-15 03:42:51 -05:00
|
|
|
if (data.message && data.message.indexOf('AUTH failed') === 0) {
|
|
|
|
location.href = serverurl + '/403'
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2016-10-10 09:04:24 -04:00
|
|
|
socket.on('delete', function () {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (personalInfo.login) {
|
2017-03-08 13:41:05 -05:00
|
|
|
deleteServerHistory(noteid, function (err, data) {
|
|
|
|
if (!err) location.href = serverurl
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
getHistory(function (notehistory) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const newnotehistory = removeHistory(noteid, notehistory)
|
2017-03-08 13:41:05 -05:00
|
|
|
saveHistory(newnotehistory)
|
|
|
|
location.href = serverurl
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-02-15 03:42:51 -05:00
|
|
|
let retryTimer = null
|
2016-06-17 04:31:36 -04:00
|
|
|
socket.on('maintenance', function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
cmClient.revision = -1
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('disconnect', function (data) {
|
2017-03-08 13:41:05 -05:00
|
|
|
showStatus(statusType.offline)
|
|
|
|
if (window.loaded) {
|
|
|
|
saveInfo()
|
2017-04-11 00:07:04 -04:00
|
|
|
lastInfo.history = editor.getHistory()
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (!editor.getOption('readOnly')) {
|
|
|
|
editor.setOption('readOnly', true)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!retryTimer) {
|
|
|
|
retryTimer = setInterval(function () {
|
2017-04-10 23:48:39 -04:00
|
|
|
if (!needRefresh) socket.connect()
|
2017-03-08 13:41:05 -05:00
|
|
|
}, 1000)
|
|
|
|
}
|
|
|
|
})
|
2015-06-01 06:04:25 -04:00
|
|
|
socket.on('reconnect', function (data) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// sync back any change in offline
|
2017-03-08 13:41:05 -05:00
|
|
|
emitUserStatus(true)
|
2017-03-28 00:15:56 -04:00
|
|
|
cursorActivity(editor)
|
2017-03-08 13:41:05 -05:00
|
|
|
socket.emit('online users')
|
|
|
|
})
|
2016-07-02 04:13:34 -04:00
|
|
|
socket.on('connect', function (data) {
|
2017-03-08 13:41:05 -05:00
|
|
|
clearInterval(retryTimer)
|
|
|
|
retryTimer = null
|
2021-02-15 03:42:51 -05:00
|
|
|
personalInfo.id = socket.id
|
2017-03-08 13:41:05 -05:00
|
|
|
showStatus(statusType.connected)
|
|
|
|
socket.emit('version')
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('version', function (data) {
|
2017-03-08 13:41:05 -05:00
|
|
|
if (version !== data.version) {
|
|
|
|
if (version < data.minimumCompatibleVersion) {
|
|
|
|
setRefreshModal('incompatible-version')
|
|
|
|
setNeedRefresh()
|
|
|
|
} else {
|
|
|
|
setRefreshModal('new-version')
|
2016-07-29 23:28:24 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
})
|
2021-02-15 03:42:51 -05:00
|
|
|
let authors = []
|
|
|
|
let authorship = []
|
|
|
|
let authorMarks = {} // temp variable
|
|
|
|
let addTextMarkers = [] // temp variable
|
2017-03-08 13:41:05 -05:00
|
|
|
function updateInfo (data) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// console.debug(data);
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(data, 'createtime') &&
|
|
|
|
window.createtime !== data.createtime
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.createtime = data.createtime
|
|
|
|
updateLastChange()
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(data, 'updatetime') &&
|
|
|
|
window.lastchangetime !== data.updatetime
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.lastchangetime = data.updatetime
|
|
|
|
updateLastChange()
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(data, 'owner') &&
|
|
|
|
window.owner !== data.owner
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.owner = data.owner
|
|
|
|
window.ownerprofile = data.ownerprofile
|
|
|
|
updateOwner()
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(data, 'lastchangeuser') &&
|
|
|
|
window.lastchangeuser !== data.lastchangeuser
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
window.lastchangeuser = data.lastchangeuser
|
|
|
|
window.lastchangeuserprofile = data.lastchangeuserprofile
|
|
|
|
updateLastChangeUser()
|
|
|
|
updateOwner()
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(data, 'authors') &&
|
|
|
|
authors !== data.authors
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
authors = data.authors
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
Object.prototype.hasOwnProperty.call(data, 'authorship') &&
|
|
|
|
authorship !== data.authorship
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
authorship = data.authorship
|
|
|
|
updateAuthorship()
|
|
|
|
}
|
2016-07-29 23:28:24 -04:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const updateAuthorship = _.debounce(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.operation(updateAuthorshipInner)
|
|
|
|
}, 50)
|
|
|
|
function initMark () {
|
|
|
|
return {
|
|
|
|
gutter: {
|
|
|
|
userid: null,
|
|
|
|
timestamp: null
|
|
|
|
},
|
|
|
|
textmarkers: []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function initMarkAndCheckGutter (mark, author, timestamp) {
|
|
|
|
if (!mark) mark = initMark()
|
|
|
|
if (!mark.gutter.userid || mark.gutter.timestamp > timestamp) {
|
|
|
|
mark.gutter.userid = author.userid
|
|
|
|
mark.gutter.timestamp = timestamp
|
|
|
|
}
|
|
|
|
return mark
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const addStyleRule = (function () {
|
|
|
|
const added = {}
|
|
|
|
const styleElement = document.createElement('style')
|
|
|
|
document.documentElement
|
|
|
|
.getElementsByTagName('head')[0]
|
|
|
|
.appendChild(styleElement)
|
|
|
|
const styleSheet = styleElement.sheet
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
return function (css) {
|
|
|
|
if (added[css]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
added[css] = true
|
2021-02-15 03:42:51 -05:00
|
|
|
styleSheet.insertRule(
|
|
|
|
css,
|
|
|
|
(styleSheet.cssRules || styleSheet.rules).length
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
})()
|
2017-03-08 13:41:05 -05:00
|
|
|
function updateAuthorshipInner () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// ignore when ot not synced yet
|
2017-03-08 13:41:05 -05:00
|
|
|
if (havePendingOperation()) return
|
|
|
|
authorMarks = {}
|
|
|
|
for (let i = 0; i < authorship.length; i++) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const atom = authorship[i]
|
|
|
|
const author = authors[atom[0]]
|
2017-03-08 13:41:05 -05:00
|
|
|
if (author) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const prePos = editor.posFromIndex(atom[1])
|
|
|
|
const preLine = editor.getLine(prePos.line)
|
|
|
|
const postPos = editor.posFromIndex(atom[2])
|
|
|
|
const postLine = editor.getLine(postPos.line)
|
2017-03-08 13:41:05 -05:00
|
|
|
if (prePos.ch === 0 && postPos.ch === postLine.length) {
|
|
|
|
for (let j = prePos.line; j <= postPos.line; j++) {
|
|
|
|
if (editor.getLine(j)) {
|
2021-02-15 03:42:51 -05:00
|
|
|
authorMarks[j] = initMarkAndCheckGutter(
|
|
|
|
authorMarks[j],
|
|
|
|
author,
|
|
|
|
atom[3]
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (postPos.line - prePos.line >= 1) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let startLine = prePos.line
|
|
|
|
let endLine = postPos.line
|
2017-03-08 13:41:05 -05:00
|
|
|
if (prePos.ch === preLine.length) {
|
|
|
|
startLine++
|
|
|
|
} else if (prePos.ch !== 0) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const mark = initMarkAndCheckGutter(
|
|
|
|
authorMarks[prePos.line],
|
|
|
|
author,
|
|
|
|
atom[3]
|
|
|
|
)
|
|
|
|
const _postPos = {
|
2017-03-08 13:41:05 -05:00
|
|
|
line: prePos.line,
|
|
|
|
ch: preLine.length
|
|
|
|
}
|
|
|
|
if (JSON.stringify(prePos) !== JSON.stringify(_postPos)) {
|
2017-03-22 05:48:26 -04:00
|
|
|
mark.textmarkers.push({
|
|
|
|
userid: author.userid,
|
|
|
|
pos: [prePos, _postPos]
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
startLine++
|
|
|
|
}
|
|
|
|
authorMarks[prePos.line] = mark
|
|
|
|
}
|
|
|
|
if (postPos.ch === 0) {
|
|
|
|
endLine--
|
|
|
|
} else if (postPos.ch !== postLine.length) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const mark = initMarkAndCheckGutter(
|
|
|
|
authorMarks[postPos.line],
|
|
|
|
author,
|
|
|
|
atom[3]
|
|
|
|
)
|
|
|
|
const _prePos = {
|
2017-03-08 13:41:05 -05:00
|
|
|
line: postPos.line,
|
|
|
|
ch: 0
|
|
|
|
}
|
|
|
|
if (JSON.stringify(_prePos) !== JSON.stringify(postPos)) {
|
2017-03-22 05:48:26 -04:00
|
|
|
mark.textmarkers.push({
|
|
|
|
userid: author.userid,
|
|
|
|
pos: [_prePos, postPos]
|
|
|
|
})
|
2017-03-08 13:41:05 -05:00
|
|
|
endLine--
|
|
|
|
}
|
|
|
|
authorMarks[postPos.line] = mark
|
|
|
|
}
|
|
|
|
for (let j = startLine; j <= endLine; j++) {
|
|
|
|
if (editor.getLine(j)) {
|
2021-02-15 03:42:51 -05:00
|
|
|
authorMarks[j] = initMarkAndCheckGutter(
|
|
|
|
authorMarks[j],
|
|
|
|
author,
|
|
|
|
atom[3]
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
const mark = initMarkAndCheckGutter(
|
|
|
|
authorMarks[prePos.line],
|
|
|
|
author,
|
|
|
|
atom[3]
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
if (JSON.stringify(prePos) !== JSON.stringify(postPos)) {
|
|
|
|
mark.textmarkers.push({
|
|
|
|
userid: author.userid,
|
|
|
|
pos: [prePos, postPos]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
authorMarks[prePos.line] = mark
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
addTextMarkers = []
|
|
|
|
editor.eachLine(iterateLine)
|
2021-02-15 03:42:51 -05:00
|
|
|
const allTextMarks = editor.getAllMarks()
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let i = 0; i < allTextMarks.length; i++) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const _textMarker = allTextMarks[i]
|
|
|
|
const pos = _textMarker.find()
|
|
|
|
let found = false
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let j = 0; j < addTextMarkers.length; j++) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const textMarker = addTextMarkers[j]
|
|
|
|
const author = authors[textMarker.userid]
|
|
|
|
const className = 'authorship-inline-' + author.color.substr(1)
|
|
|
|
const obj = {
|
2017-03-08 13:41:05 -05:00
|
|
|
from: textMarker.pos[0],
|
|
|
|
to: textMarker.pos[1]
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
JSON.stringify(pos) === JSON.stringify(obj) &&
|
|
|
|
_textMarker.className &&
|
|
|
|
_textMarker.className.indexOf(className) > -1
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
addTextMarkers.splice(j, 1)
|
|
|
|
j--
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
!found &&
|
|
|
|
_textMarker.className &&
|
|
|
|
_textMarker.className.indexOf('authorship-inline') > -1
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
_textMarker.clear()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (let i = 0; i < addTextMarkers.length; i++) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const textMarker = addTextMarkers[i]
|
|
|
|
const author = authors[textMarker.userid]
|
2017-03-28 08:38:31 -04:00
|
|
|
const rgbcolor = hex2rgb(author.color)
|
|
|
|
const colorString = `rgba(${rgbcolor.red},${rgbcolor.green},${rgbcolor.blue},0.7)`
|
|
|
|
const styleString = `background-image: linear-gradient(to top, ${colorString} 1px, transparent 1px);`
|
2021-02-15 03:42:51 -05:00
|
|
|
const className = `authorship-inline-${author.color.substr(1)}`
|
2017-03-28 08:38:31 -04:00
|
|
|
const rule = `.${className} { ${styleString} }`
|
2017-03-08 13:41:05 -05:00
|
|
|
addStyleRule(rule)
|
|
|
|
editor.markText(textMarker.pos[0], textMarker.pos[1], {
|
|
|
|
className: 'authorship-inline ' + className,
|
|
|
|
title: author.name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
function iterateLine (line) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const lineNumber = line.lineNo()
|
|
|
|
const currMark = authorMarks[lineNumber]
|
|
|
|
const author = currMark ? authors[currMark.gutter.userid] : null
|
2017-03-08 13:41:05 -05:00
|
|
|
if (currMark && author) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const className = 'authorship-gutter-' + author.color.substr(1)
|
2017-03-28 08:38:31 -04:00
|
|
|
const gutters = line.gutterMarkers
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
!gutters ||
|
|
|
|
!gutters['authorship-gutters'] ||
|
|
|
|
!gutters['authorship-gutters'].className ||
|
|
|
|
!gutters['authorship-gutters'].className.indexOf(className) < 0
|
|
|
|
) {
|
2017-03-28 08:38:31 -04:00
|
|
|
const styleString = `border-left: 3px solid ${author.color}; height: ${defaultTextHeight}px; margin-left: 3px;`
|
|
|
|
const rule = `.${className} { ${styleString} }`
|
2017-03-08 13:41:05 -05:00
|
|
|
addStyleRule(rule)
|
2021-02-15 03:42:51 -05:00
|
|
|
const gutter = $('<div>', {
|
2017-03-08 13:41:05 -05:00
|
|
|
class: 'authorship-gutter ' + className,
|
|
|
|
title: author.name
|
|
|
|
})
|
|
|
|
editor.setGutterMarker(line, 'authorship-gutters', gutter[0])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
editor.setGutterMarker(line, 'authorship-gutters', null)
|
|
|
|
}
|
|
|
|
if (currMark && currMark.textmarkers.length > 0) {
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < currMark.textmarkers.length; i++) {
|
|
|
|
const textMarker = currMark.textmarkers[i]
|
2017-03-08 13:41:05 -05:00
|
|
|
if (textMarker.userid !== currMark.gutter.userid) {
|
|
|
|
addTextMarkers.push(textMarker)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 04:22:50 -04:00
|
|
|
}
|
2017-03-27 23:18:36 -04:00
|
|
|
editorInstance.on('update', function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
$('.authorship-gutter:not([data-original-title])').tooltip({
|
|
|
|
container: '.CodeMirror-lines',
|
|
|
|
placement: 'right',
|
2021-02-15 03:42:51 -05:00
|
|
|
delay: { show: 500, hide: 100 }
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
$('.authorship-inline:not([data-original-title])').tooltip({
|
|
|
|
container: '.CodeMirror-lines',
|
|
|
|
placement: 'bottom',
|
2021-02-15 03:42:51 -05:00
|
|
|
delay: { show: 500, hide: 100 }
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2017-03-28 08:38:31 -04:00
|
|
|
// clear tooltip which described element has been removed
|
2017-03-08 13:41:05 -05:00
|
|
|
$('[id^="tooltip"]').each(function (index, element) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const $ele = $(element)
|
|
|
|
if ($('[aria-describedby="' + $ele.attr('id') + '"]').length <= 0) { $ele.remove() }
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
socket.on('check', function (data) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// console.debug(data);
|
2017-03-08 13:41:05 -05:00
|
|
|
updateInfo(data)
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
socket.on('permission', function (data) {
|
2017-03-08 13:41:05 -05:00
|
|
|
updatePermission(data.permission)
|
|
|
|
})
|
2017-03-28 05:16:32 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let permission = null
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('refresh', function (data) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// console.debug(data);
|
2017-04-09 09:14:23 -04:00
|
|
|
editorInstance.config.docmaxlength = data.docmaxlength
|
|
|
|
editor.setOption('maxLength', editorInstance.config.docmaxlength)
|
2017-03-08 13:41:05 -05:00
|
|
|
updateInfo(data)
|
|
|
|
updatePermission(data.permission)
|
|
|
|
if (!window.loaded) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// auto change mode if no content detected
|
2021-02-15 03:42:51 -05:00
|
|
|
const nocontent = editor.getValue().length <= 0
|
2017-03-08 13:41:05 -05:00
|
|
|
if (nocontent) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (visibleXS) {
|
|
|
|
appState.currentMode = modeType.edit
|
|
|
|
} else {
|
|
|
|
appState.currentMode = modeType.both
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2017-03-28 08:38:31 -04:00
|
|
|
// parse mode from url
|
2017-03-08 13:41:05 -05:00
|
|
|
if (window.location.search.length > 0) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const urlMode = modeType[window.location.search.substr(1)]
|
2017-04-11 21:21:13 -04:00
|
|
|
if (urlMode) appState.currentMode = urlMode
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2017-04-11 21:21:13 -04:00
|
|
|
changeMode(appState.currentMode)
|
2017-04-10 23:48:39 -04:00
|
|
|
if (nocontent && !visibleXS) {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.focus()
|
|
|
|
editor.refresh()
|
|
|
|
}
|
|
|
|
updateViewInner() // bring up view rendering earlier
|
|
|
|
updateHistory() // update history whether have content or not
|
|
|
|
window.loaded = true
|
|
|
|
emitUserStatus() // send first user status
|
|
|
|
updateOnlineStatus() // update first online status
|
|
|
|
setTimeout(function () {
|
2017-03-28 08:38:31 -04:00
|
|
|
// work around editor not refresh or doc not fully loaded
|
2017-03-08 13:41:05 -05:00
|
|
|
windowResizeInner()
|
2017-03-28 08:38:31 -04:00
|
|
|
// work around might not scroll to hash
|
2017-03-08 13:41:05 -05:00
|
|
|
scrollToHash()
|
|
|
|
}, 1)
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (editor.getOption('readOnly')) {
|
|
|
|
editor.setOption('readOnly', false)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2015-07-11 00:43:08 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const EditorClient = ot.EditorClient
|
|
|
|
const SocketIOAdapter = ot.SocketIOAdapter
|
|
|
|
const CodeMirrorAdapter = ot.CodeMirrorAdapter
|
|
|
|
let cmClient = null
|
|
|
|
let synchronized_ = null
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function havePendingOperation () {
|
2021-02-15 03:42:51 -05:00
|
|
|
return !!(
|
|
|
|
cmClient &&
|
|
|
|
cmClient.state &&
|
|
|
|
Object.prototype.hasOwnProperty.call(cmClient, 'outstanding')
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
socket.on('doc', function (obj) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const body = obj.str
|
|
|
|
const bodyMismatch = editor.getValue() !== body
|
|
|
|
const setDoc =
|
|
|
|
!cmClient ||
|
|
|
|
(cmClient &&
|
|
|
|
(cmClient.revision === -1 ||
|
|
|
|
(cmClient.revision !== obj.revision && !havePendingOperation()))) ||
|
|
|
|
obj.force
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
saveInfo()
|
|
|
|
if (setDoc && bodyMismatch) {
|
|
|
|
if (cmClient) cmClient.editorAdapter.ignoreNextChange = true
|
|
|
|
if (body) editor.setValue(body)
|
|
|
|
else editor.setValue('')
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!window.loaded) {
|
|
|
|
editor.clearHistory()
|
|
|
|
ui.spinner.hide()
|
|
|
|
ui.content.fadeIn()
|
|
|
|
} else {
|
2017-03-28 08:38:31 -04:00
|
|
|
// if current doc is equal to the doc before disconnect
|
2017-03-08 13:41:05 -05:00
|
|
|
if (setDoc && bodyMismatch) editor.clearHistory()
|
2017-04-11 00:07:04 -04:00
|
|
|
else if (lastInfo.history) editor.setHistory(lastInfo.history)
|
|
|
|
lastInfo.history = null
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cmClient) {
|
|
|
|
cmClient = window.cmClient = new EditorClient(
|
2021-02-15 03:42:51 -05:00
|
|
|
obj.revision,
|
|
|
|
obj.clients,
|
|
|
|
new SocketIOAdapter(socket),
|
|
|
|
new CodeMirrorAdapter(editor)
|
2017-03-28 08:38:31 -04:00
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
synchronized_ = cmClient.state
|
|
|
|
} else if (setDoc) {
|
|
|
|
if (bodyMismatch) {
|
|
|
|
cmClient.undoManager.undoStack.length = 0
|
|
|
|
cmClient.undoManager.redoStack.length = 0
|
|
|
|
}
|
|
|
|
cmClient.revision = obj.revision
|
|
|
|
cmClient.setState(synchronized_)
|
|
|
|
cmClient.initializeClientList()
|
|
|
|
cmClient.initializeClients(obj.clients)
|
|
|
|
} else if (havePendingOperation()) {
|
|
|
|
cmClient.serverReconnect()
|
|
|
|
}
|
|
|
|
|
|
|
|
if (setDoc && bodyMismatch) {
|
2017-04-10 23:48:39 -04:00
|
|
|
isDirty = true
|
2017-03-08 13:41:05 -05:00
|
|
|
updateView()
|
|
|
|
}
|
|
|
|
|
|
|
|
restoreInfo()
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2015-09-25 06:01:15 -04:00
|
|
|
socket.on('ack', function () {
|
2017-04-10 23:48:39 -04:00
|
|
|
isDirty = true
|
2017-03-08 13:41:05 -05:00
|
|
|
updateView()
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2015-09-25 06:01:15 -04:00
|
|
|
socket.on('operation', function () {
|
2017-04-10 23:48:39 -04:00
|
|
|
isDirty = true
|
2017-03-08 13:41:05 -05:00
|
|
|
updateView()
|
|
|
|
})
|
2015-07-01 12:10:20 -04:00
|
|
|
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('online users', function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (debug) {
|
|
|
|
console.debug(data)
|
|
|
|
}
|
2017-04-11 00:07:04 -04:00
|
|
|
onlineUsers = data.users
|
2017-03-08 13:41:05 -05:00
|
|
|
updateOnlineStatus()
|
2021-02-15 03:42:51 -05:00
|
|
|
$('.CodeMirror-other-cursors')
|
|
|
|
.children()
|
|
|
|
.each(function (key, value) {
|
|
|
|
let found = false
|
|
|
|
for (let i = 0; i < data.users.length; i++) {
|
|
|
|
const user = data.users[i]
|
|
|
|
if ($(this).attr('id') === user.id) {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
$(this)
|
|
|
|
.stop(true)
|
|
|
|
.fadeOut('normal', function () {
|
|
|
|
$(this).remove()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
for (let i = 0; i < data.users.length; i++) {
|
|
|
|
const user = data.users[i]
|
|
|
|
if (user.id !== socket.id) {
|
|
|
|
buildCursor(user)
|
|
|
|
} else {
|
|
|
|
personalInfo = user
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2015-06-01 06:04:25 -04:00
|
|
|
socket.on('user status', function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (debug) {
|
|
|
|
console.debug(data)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === data.id) {
|
|
|
|
onlineUsers[i] = data
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
updateOnlineStatus()
|
2021-02-15 03:42:51 -05:00
|
|
|
if (data.id !== socket.id) {
|
|
|
|
buildCursor(data)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('cursor focus', function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (debug) {
|
|
|
|
console.debug(data)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === data.id) {
|
|
|
|
onlineUsers[i].cursor = data.cursor
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (data.id !== socket.id) {
|
|
|
|
buildCursor(data)
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// force show
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursor = $('div[data-clientid="' + data.id + '"]')
|
2017-03-08 13:41:05 -05:00
|
|
|
if (cursor.length > 0) {
|
|
|
|
cursor.stop(true).fadeIn()
|
|
|
|
}
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('cursor activity', function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (debug) {
|
|
|
|
console.debug(data)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === data.id) {
|
|
|
|
onlineUsers[i].cursor = data.cursor
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (data.id !== socket.id) {
|
|
|
|
buildCursor(data)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
socket.on('cursor blur', function (data) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (debug) {
|
|
|
|
console.debug(data)
|
|
|
|
}
|
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === data.id) {
|
|
|
|
onlineUsers[i].cursor = null
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (data.id !== socket.id) {
|
|
|
|
buildCursor(data)
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// force hide
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursor = $('div[data-clientid="' + data.id + '"]')
|
2017-03-08 13:41:05 -05:00
|
|
|
if (cursor.length > 0) {
|
|
|
|
cursor.stop(true).fadeOut()
|
|
|
|
}
|
|
|
|
})
|
2015-05-15 00:58:13 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const options = {
|
2017-03-08 13:41:05 -05:00
|
|
|
valueNames: ['id', 'name'],
|
2021-02-15 03:42:51 -05:00
|
|
|
item:
|
|
|
|
'<li class="ui-user-item">' +
|
|
|
|
'<span class="id" style="display:none;"></span>' +
|
|
|
|
'<a href="#">' +
|
|
|
|
'<span class="pull-left"><i class="ui-user-icon"></i></span><span class="ui-user-name name"></span><span class="pull-right"><i class="fa fa-circle ui-user-status"></i></span>' +
|
|
|
|
'</a>' +
|
|
|
|
'</li>'
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const onlineUserList = new List('online-user-list', options)
|
|
|
|
const shortOnlineUserList = new List('short-online-user-list', options)
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
function updateOnlineStatus () {
|
|
|
|
if (!window.loaded || !socket.connected) return
|
2021-02-15 03:42:51 -05:00
|
|
|
const _onlineUsers = deduplicateOnlineUsers(onlineUsers)
|
2017-03-08 13:41:05 -05:00
|
|
|
showStatus(statusType.online, _onlineUsers.length)
|
2021-02-15 03:42:51 -05:00
|
|
|
const items = onlineUserList.items
|
2019-05-30 18:27:56 -04:00
|
|
|
// update or remove current list items
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let i = 0; i < items.length; i++) {
|
|
|
|
let found = false
|
|
|
|
let foundindex = null
|
|
|
|
for (let j = 0; j < _onlineUsers.length; j++) {
|
|
|
|
if (items[i].values().id === _onlineUsers[j].id) {
|
|
|
|
foundindex = j
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const id = items[i].values().id
|
2017-03-08 13:41:05 -05:00
|
|
|
if (found) {
|
|
|
|
onlineUserList.get('id', id)[0].values(_onlineUsers[foundindex])
|
|
|
|
shortOnlineUserList.get('id', id)[0].values(_onlineUsers[foundindex])
|
|
|
|
} else {
|
|
|
|
onlineUserList.remove('id', id)
|
|
|
|
shortOnlineUserList.remove('id', id)
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// add not in list items
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let i = 0; i < _onlineUsers.length; i++) {
|
|
|
|
let found = false
|
|
|
|
for (let j = 0; j < items.length; j++) {
|
|
|
|
if (items[j].values().id === _onlineUsers[i].id) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found) {
|
|
|
|
onlineUserList.add(_onlineUsers[i])
|
|
|
|
shortOnlineUserList.add(_onlineUsers[i])
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// sorting
|
2017-03-08 13:41:05 -05:00
|
|
|
sortOnlineUserList(onlineUserList)
|
|
|
|
sortOnlineUserList(shortOnlineUserList)
|
2019-05-30 18:27:56 -04:00
|
|
|
// render list items
|
2017-03-08 13:41:05 -05:00
|
|
|
renderUserStatusList(onlineUserList)
|
|
|
|
renderUserStatusList(shortOnlineUserList)
|
|
|
|
}
|
|
|
|
|
|
|
|
function sortOnlineUserList (list) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// sort order by isSelf, login state, idle state, alphabet name, color brightness
|
2017-03-08 13:41:05 -05:00
|
|
|
list.sort('', {
|
|
|
|
sortFunction: function (a, b) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const usera = a.values()
|
|
|
|
const userb = b.values()
|
|
|
|
const useraIsSelf =
|
|
|
|
usera.id === personalInfo.id ||
|
|
|
|
(usera.login && usera.userid === personalInfo.userid)
|
|
|
|
const userbIsSelf =
|
|
|
|
userb.id === personalInfo.id ||
|
|
|
|
(userb.login && userb.userid === personalInfo.userid)
|
2017-03-08 13:41:05 -05:00
|
|
|
if (useraIsSelf && !userbIsSelf) {
|
|
|
|
return -1
|
|
|
|
} else if (!useraIsSelf && userbIsSelf) {
|
|
|
|
return 1
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (usera.login && !userb.login) {
|
|
|
|
return -1
|
|
|
|
} else if (!usera.login && userb.login) {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
if (!usera.idle && userb.idle) {
|
|
|
|
return -1
|
|
|
|
} else if (usera.idle && !userb.idle) {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
if (
|
|
|
|
usera.name &&
|
|
|
|
userb.name &&
|
|
|
|
usera.name.toLowerCase() < userb.name.toLowerCase()
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
return -1
|
2021-02-15 03:42:51 -05:00
|
|
|
} else if (
|
|
|
|
usera.name &&
|
|
|
|
userb.name &&
|
|
|
|
usera.name.toLowerCase() > userb.name.toLowerCase()
|
|
|
|
) {
|
2017-03-22 05:48:26 -04:00
|
|
|
return 1
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
usera.color &&
|
|
|
|
userb.color &&
|
|
|
|
usera.color.toLowerCase() < userb.color.toLowerCase()
|
|
|
|
) {
|
|
|
|
return -1
|
|
|
|
} else if (
|
|
|
|
usera.color &&
|
|
|
|
userb.color &&
|
|
|
|
usera.color.toLowerCase() > userb.color.toLowerCase()
|
|
|
|
) {
|
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
2017-03-22 05:48:26 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderUserStatusList (list) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const items = list.items
|
|
|
|
for (let j = 0; j < items.length; j++) {
|
|
|
|
const item = items[j]
|
|
|
|
const userstatus = $(item.elm).find('.ui-user-status')
|
|
|
|
const usericon = $(item.elm).find('.ui-user-icon')
|
2017-03-08 13:41:05 -05:00
|
|
|
if (item.values().login && item.values().photo) {
|
|
|
|
usericon.css('background-image', 'url(' + item.values().photo + ')')
|
2019-05-30 18:27:56 -04:00
|
|
|
// add 1px more to right, make it feel aligned
|
2017-03-08 13:41:05 -05:00
|
|
|
usericon.css('margin-right', '6px')
|
|
|
|
$(item.elm).css('border-left', '4px solid ' + item.values().color)
|
|
|
|
usericon.css('margin-left', '-4px')
|
|
|
|
} else {
|
|
|
|
usericon.css('background-color', item.values().color)
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
userstatus.removeClass(
|
|
|
|
'ui-user-status-offline ui-user-status-online ui-user-status-idle'
|
|
|
|
)
|
|
|
|
if (item.values().idle) {
|
|
|
|
userstatus.addClass('ui-user-status-idle')
|
|
|
|
} else {
|
|
|
|
userstatus.addClass('ui-user-status-online')
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function deduplicateOnlineUsers (list) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const _onlineUsers = []
|
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
const user = $.extend({}, list[i])
|
|
|
|
if (!user.userid) {
|
|
|
|
_onlineUsers.push(user)
|
|
|
|
} else {
|
|
|
|
let found = false
|
|
|
|
for (let j = 0; j < _onlineUsers.length; j++) {
|
2017-03-08 13:41:05 -05:00
|
|
|
if (_onlineUsers[j].userid === user.userid) {
|
2017-03-22 05:48:26 -04:00
|
|
|
// keep self color when login
|
2017-04-11 00:07:04 -04:00
|
|
|
if (user.id === personalInfo.id) {
|
2017-03-08 13:41:05 -05:00
|
|
|
_onlineUsers[j].color = user.color
|
|
|
|
}
|
2017-03-22 05:48:26 -04:00
|
|
|
// keep idle state if any of self client not idle
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!user.idle) {
|
|
|
|
_onlineUsers[j].idle = user.idle
|
|
|
|
_onlineUsers[j].color = user.color
|
|
|
|
}
|
|
|
|
found = true
|
|
|
|
break
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (!found) {
|
|
|
|
_onlineUsers.push(user)
|
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
return _onlineUsers
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let userStatusCache = null
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function emitUserStatus (force) {
|
|
|
|
if (!window.loaded) return
|
2021-02-15 03:42:51 -05:00
|
|
|
let type = null
|
|
|
|
if (visibleXS) {
|
|
|
|
type = 'xs'
|
|
|
|
} else if (visibleSM) {
|
|
|
|
type = 'sm'
|
|
|
|
} else if (visibleMD) {
|
|
|
|
type = 'md'
|
|
|
|
} else if (visibleLG) {
|
|
|
|
type = 'lg'
|
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
personalInfo.idle = idle.isAway
|
|
|
|
personalInfo.type = type
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === personalInfo.id) {
|
|
|
|
onlineUsers[i] = personalInfo
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const userStatus = {
|
2017-03-08 13:41:05 -05:00
|
|
|
idle: idle.isAway,
|
|
|
|
type: type
|
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
if (force || JSON.stringify(userStatus) !== JSON.stringify(userStatusCache)) {
|
|
|
|
socket.emit('user status', userStatus)
|
|
|
|
userStatusCache = userStatus
|
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkCursorTag (coord, ele) {
|
|
|
|
if (!ele) return // return if element not exists
|
2017-03-22 05:48:26 -04:00
|
|
|
// set margin
|
2021-02-15 03:42:51 -05:00
|
|
|
const tagRightMargin = 0
|
|
|
|
const tagBottomMargin = 2
|
2017-03-22 05:48:26 -04:00
|
|
|
// use sizer to get the real doc size (won't count status bar and gutters)
|
2021-02-15 03:42:51 -05:00
|
|
|
const docWidth = ui.area.codemirrorSizer.width()
|
2017-03-22 05:48:26 -04:00
|
|
|
// get editor size (status bar not count in)
|
2021-02-15 03:42:51 -05:00
|
|
|
const editorHeight = ui.area.codemirror.height()
|
2017-03-22 05:48:26 -04:00
|
|
|
// get element size
|
2021-02-15 03:42:51 -05:00
|
|
|
const width = ele.outerWidth()
|
|
|
|
const height = ele.outerHeight()
|
|
|
|
const padding = (ele.outerWidth() - ele.width()) / 2
|
2017-03-22 05:48:26 -04:00
|
|
|
// get coord position
|
2021-02-15 03:42:51 -05:00
|
|
|
const left = coord.left
|
|
|
|
const top = coord.top
|
2017-03-22 05:48:26 -04:00
|
|
|
// get doc top offset (to workaround with viewport)
|
2021-02-15 03:42:51 -05:00
|
|
|
const docTopOffset = ui.area.codemirrorSizerInner.position().top
|
2017-03-22 05:48:26 -04:00
|
|
|
// set offset
|
2021-02-15 03:42:51 -05:00
|
|
|
let offsetLeft = -3
|
|
|
|
let offsetTop = defaultTextHeight
|
2017-03-22 05:48:26 -04:00
|
|
|
// only do when have width and height
|
2017-03-08 13:41:05 -05:00
|
|
|
if (width > 0 && height > 0) {
|
2017-03-22 05:48:26 -04:00
|
|
|
// flip x when element right bound larger than doc width
|
2017-03-08 13:41:05 -05:00
|
|
|
if (left + width + offsetLeft + tagRightMargin > docWidth) {
|
|
|
|
offsetLeft = -(width + tagRightMargin) + padding + offsetLeft
|
|
|
|
}
|
2017-03-22 05:48:26 -04:00
|
|
|
// flip y when element bottom bound larger than doc height
|
|
|
|
// and element top position is larger than element height
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
top + docTopOffset + height + offsetTop + tagBottomMargin >
|
|
|
|
Math.max(editor.doc.height, editorHeight) &&
|
|
|
|
top + docTopOffset > height + tagBottomMargin
|
|
|
|
) {
|
|
|
|
offsetTop = -height
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2017-03-22 05:48:26 -04:00
|
|
|
// set position
|
2017-03-08 13:41:05 -05:00
|
|
|
ele[0].style.left = offsetLeft + 'px'
|
|
|
|
ele[0].style.top = offsetTop + 'px'
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildCursor (user) {
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode === modeType.view) return
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!user.cursor) return
|
2021-02-15 03:42:51 -05:00
|
|
|
const coord = editor.charCoords(user.cursor, 'windows')
|
2017-03-08 13:41:05 -05:00
|
|
|
coord.left = coord.left < 4 ? 4 : coord.left
|
|
|
|
coord.top = coord.top < 0 ? 0 : coord.top
|
2021-02-15 03:42:51 -05:00
|
|
|
let iconClass = 'fa-user'
|
2017-03-08 13:41:05 -05:00
|
|
|
switch (user.type) {
|
|
|
|
case 'xs':
|
|
|
|
iconClass = 'fa-mobile'
|
|
|
|
break
|
|
|
|
case 'sm':
|
|
|
|
iconClass = 'fa-tablet'
|
|
|
|
break
|
|
|
|
case 'md':
|
|
|
|
iconClass = 'fa-desktop'
|
|
|
|
break
|
|
|
|
case 'lg':
|
|
|
|
iconClass = 'fa-desktop'
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if ($('div[data-clientid="' + user.id + '"]').length <= 0) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursor = $(
|
|
|
|
'<div data-clientid="' +
|
|
|
|
user.id +
|
|
|
|
'" class="CodeMirror-other-cursor" style="display:none;"></div>'
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
cursor.attr('data-line', user.cursor.line)
|
|
|
|
cursor.attr('data-ch', user.cursor.ch)
|
|
|
|
cursor.attr('data-offset-left', 0)
|
|
|
|
cursor.attr('data-offset-top', 0)
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursorbar = $('<div class="cursorbar"> </div>')
|
2017-03-08 13:41:05 -05:00
|
|
|
cursorbar[0].style.height = defaultTextHeight + 'px'
|
|
|
|
cursorbar[0].style.borderLeft = '2px solid ' + user.color
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const icon = '<i class="fa ' + iconClass + '"></i>'
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursortag = $(
|
|
|
|
'<div class="cursortag">' +
|
|
|
|
icon +
|
|
|
|
' <span class="name">' +
|
|
|
|
user.name +
|
|
|
|
'</span></div>'
|
|
|
|
)
|
2017-03-22 05:48:26 -04:00
|
|
|
// cursortag[0].style.background = color;
|
2017-03-08 13:41:05 -05:00
|
|
|
cursortag[0].style.color = user.color
|
|
|
|
|
|
|
|
cursor.attr('data-mode', 'hover')
|
|
|
|
cursortag.delay(2000).fadeOut('fast')
|
|
|
|
cursor.hover(
|
2017-04-10 23:37:41 -04:00
|
|
|
function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (cursor.attr('data-mode') === 'hover') {
|
|
|
|
cursortag.stop(true).fadeIn('fast')
|
|
|
|
}
|
2017-04-10 23:37:41 -04:00
|
|
|
},
|
|
|
|
function () {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (cursor.attr('data-mode') === 'hover') {
|
|
|
|
cursortag.stop(true).fadeOut('fast')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const hideCursorTagDelay = 2000
|
|
|
|
let hideCursorTagTimer = null
|
2015-06-01 06:04:25 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const switchMode = function (ele) {
|
|
|
|
if (ele.attr('data-mode') === 'state') {
|
|
|
|
ele.attr('data-mode', 'hover')
|
|
|
|
} else if (ele.attr('data-mode') === 'hover') {
|
|
|
|
ele.attr('data-mode', 'state')
|
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const switchTag = function (ele) {
|
|
|
|
if (ele.css('display') === 'none') {
|
|
|
|
ele.stop(true).fadeIn('fast')
|
|
|
|
} else {
|
|
|
|
ele.stop(true).fadeOut('fast')
|
|
|
|
}
|
2017-01-01 22:05:05 -05:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const hideCursorTag = function () {
|
|
|
|
if (cursor.attr('data-mode') === 'hover') {
|
|
|
|
cursortag.fadeOut('fast')
|
|
|
|
}
|
2015-07-16 10:46:06 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
cursor.on('touchstart', function (e) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const display = cursortag.css('display')
|
2017-03-08 13:41:05 -05:00
|
|
|
cursortag.stop(true).fadeIn('fast')
|
|
|
|
clearTimeout(hideCursorTagTimer)
|
|
|
|
hideCursorTagTimer = setTimeout(hideCursorTag, hideCursorTagDelay)
|
|
|
|
if (display === 'none') {
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
cursortag.on('mousedown touchstart', function (e) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (cursor.attr('data-mode') === 'state') {
|
|
|
|
switchTag(cursortag)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
switchMode(cursor)
|
|
|
|
e.preventDefault()
|
|
|
|
e.stopPropagation()
|
|
|
|
})
|
|
|
|
|
|
|
|
cursor.append(cursorbar)
|
|
|
|
cursor.append(cursortag)
|
|
|
|
|
|
|
|
cursor[0].style.left = coord.left + 'px'
|
|
|
|
cursor[0].style.top = coord.top + 'px'
|
|
|
|
$('.CodeMirror-other-cursors').append(cursor)
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
if (!user.idle) {
|
|
|
|
cursor.stop(true).fadeIn()
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
checkCursorTag(coord, cursortag)
|
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursor = $('div[data-clientid="' + user.id + '"]')
|
2017-03-08 13:41:05 -05:00
|
|
|
cursor.attr('data-line', user.cursor.line)
|
|
|
|
cursor.attr('data-ch', user.cursor.ch)
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursorbar = cursor.find('.cursorbar')
|
2017-03-08 13:41:05 -05:00
|
|
|
cursorbar[0].style.height = defaultTextHeight + 'px'
|
|
|
|
cursorbar[0].style.borderLeft = '2px solid ' + user.color
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursortag = cursor.find('.cursortag')
|
2017-03-08 13:41:05 -05:00
|
|
|
cursortag.find('i').removeClass().addClass('fa').addClass(iconClass)
|
|
|
|
cursortag.find('.name').text(user.name)
|
|
|
|
|
|
|
|
if (cursor.css('display') === 'none') {
|
|
|
|
cursor[0].style.left = coord.left + 'px'
|
|
|
|
cursor[0].style.top = coord.top + 'px'
|
2015-09-25 06:48:45 -04:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
cursor.animate(
|
|
|
|
{
|
|
|
|
left: coord.left,
|
|
|
|
top: coord.top
|
|
|
|
},
|
|
|
|
{
|
|
|
|
duration: cursorAnimatePeriod,
|
|
|
|
queue: false
|
|
|
|
}
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
if (user.idle && cursor.css('display') !== 'none') {
|
|
|
|
cursor.stop(true).fadeOut()
|
|
|
|
} else if (!user.idle && cursor.css('display') === 'none') {
|
|
|
|
cursor.stop(true).fadeIn()
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
checkCursorTag(coord, cursortag)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// editor actions
|
|
|
|
function removeNullByte (cm, change) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const str = change.text.join('\n')
|
2018-11-14 08:10:14 -05:00
|
|
|
// eslint-disable-next-line no-control-regex
|
2017-03-08 13:41:05 -05:00
|
|
|
if (/\u0000/g.test(str) && change.update) {
|
2021-02-15 03:42:51 -05:00
|
|
|
change.update(
|
|
|
|
change.from,
|
|
|
|
change.to,
|
|
|
|
// eslint-disable-next-line no-control-regex
|
|
|
|
str.replace(/\u0000/g, '').split('\n')
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
function enforceMaxLength (cm, change) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const maxLength = cm.getOption('maxLength')
|
2017-03-08 13:41:05 -05:00
|
|
|
if (maxLength && change.update) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let str = change.text.join('\n')
|
|
|
|
let delta =
|
|
|
|
str.length - (cm.indexFromPos(change.to) - cm.indexFromPos(change.from))
|
2017-03-08 13:41:05 -05:00
|
|
|
if (delta <= 0) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
delta = cm.getValue().length + delta - maxLength
|
|
|
|
if (delta > 0) {
|
|
|
|
str = str.substr(0, str.length - delta)
|
|
|
|
change.update(change.from, change.to, str.split('\n'))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const ignoreEmitEvents = ['setValue', 'ignoreHistory']
|
2017-03-27 23:18:36 -04:00
|
|
|
editorInstance.on('beforeChange', function (cm, change) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (debug) {
|
|
|
|
console.debug(change)
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
removeNullByte(cm, change)
|
|
|
|
if (enforceMaxLength(cm, change)) {
|
|
|
|
$('.limit-modal').modal('show')
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const isIgnoreEmitEvent = ignoreEmitEvents.indexOf(change.origin) !== -1
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!isIgnoreEmitEvent) {
|
|
|
|
if (!havePermission()) {
|
|
|
|
change.canceled = true
|
|
|
|
switch (permission) {
|
|
|
|
case 'editable':
|
|
|
|
$('.signin-modal').modal('show')
|
|
|
|
break
|
|
|
|
case 'locked':
|
|
|
|
case 'private':
|
|
|
|
$('.locked-modal').modal('show')
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (change.origin === 'ignoreHistory') {
|
|
|
|
setHaveUnreadChanges(true)
|
|
|
|
updateTitleReminder()
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (cmClient && !socket.connected) {
|
|
|
|
cmClient.editorAdapter.ignoreNextChange = true
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2017-03-27 23:18:36 -04:00
|
|
|
editorInstance.on('cut', function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// na
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2017-03-27 23:18:36 -04:00
|
|
|
editorInstance.on('paste', function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// na
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
2017-03-28 05:32:42 -04:00
|
|
|
editorInstance.on('changes', function (editor, changes) {
|
2017-03-08 13:41:05 -05:00
|
|
|
updateHistory()
|
2021-02-15 03:42:51 -05:00
|
|
|
const docLength = editor.getValue().length
|
2019-05-30 18:27:56 -04:00
|
|
|
// workaround for big documents
|
2021-02-15 03:42:51 -05:00
|
|
|
let newViewportMargin = 20
|
2017-03-08 13:41:05 -05:00
|
|
|
if (docLength > 20000) {
|
|
|
|
newViewportMargin = 1
|
|
|
|
} else if (docLength > 10000) {
|
|
|
|
newViewportMargin = 10
|
|
|
|
} else if (docLength > 5000) {
|
|
|
|
newViewportMargin = 15
|
|
|
|
}
|
|
|
|
if (newViewportMargin !== viewportMargin) {
|
|
|
|
viewportMargin = newViewportMargin
|
|
|
|
windowResize()
|
|
|
|
}
|
|
|
|
checkEditorScrollbar()
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
ui.area.codemirrorScroll[0].scrollHeight > ui.area.view[0].scrollHeight &&
|
|
|
|
editorHasFocus()
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
postUpdateEvent = function () {
|
|
|
|
syncScrollToView()
|
|
|
|
postUpdateEvent = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2017-03-28 00:11:05 -04:00
|
|
|
editorInstance.on('focus', function (editor) {
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === personalInfo.id) {
|
|
|
|
onlineUsers[i].cursor = editor.getCursor()
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
personalInfo.cursor = editor.getCursor()
|
2017-03-08 13:41:05 -05:00
|
|
|
socket.emit('cursor focus', editor.getCursor())
|
|
|
|
})
|
2017-03-28 00:10:35 -04:00
|
|
|
|
|
|
|
const cursorActivity = _.debounce(cursorActivityInner, cursorActivityDebounce)
|
|
|
|
|
2017-03-28 00:15:56 -04:00
|
|
|
function cursorActivityInner (editor) {
|
2017-03-28 00:10:35 -04:00
|
|
|
if (editorHasFocus() && !Visibility.hidden()) {
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === personalInfo.id) {
|
|
|
|
onlineUsers[i].cursor = editor.getCursor()
|
2017-03-28 00:10:35 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
personalInfo.cursor = editor.getCursor()
|
2017-03-28 00:10:35 -04:00
|
|
|
socket.emit('cursor activity', editor.getCursor())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 05:16:32 -04:00
|
|
|
editorInstance.on('cursorActivity', editorInstance.updateStatusBar)
|
2017-03-28 00:10:35 -04:00
|
|
|
editorInstance.on('cursorActivity', cursorActivity)
|
2017-03-27 23:57:44 -04:00
|
|
|
|
2017-03-28 05:16:32 -04:00
|
|
|
editorInstance.on('beforeSelectionChange', editorInstance.updateStatusBar)
|
2017-03-27 23:18:36 -04:00
|
|
|
editorInstance.on('beforeSelectionChange', function (doc, selections) {
|
2017-03-27 23:57:44 -04:00
|
|
|
// check selection and whether the statusbar has added
|
|
|
|
if (selections && editorInstance.statusSelection) {
|
|
|
|
const selection = selections.ranges[0]
|
|
|
|
|
|
|
|
const anchor = selection.anchor
|
|
|
|
const head = selection.head
|
|
|
|
const start = head.line <= anchor.line ? head : anchor
|
|
|
|
const end = head.line >= anchor.line ? head : anchor
|
|
|
|
const selectionCharCount = Math.abs(head.ch - anchor.ch)
|
|
|
|
|
|
|
|
let selectionText = ' — Selected '
|
|
|
|
|
|
|
|
// borrow from brackets EditorStatusBar.js
|
|
|
|
if (start.line !== end.line) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let lines = end.line - start.line + 1
|
2017-03-27 23:57:44 -04:00
|
|
|
if (end.ch === 0) {
|
|
|
|
lines--
|
|
|
|
}
|
|
|
|
selectionText += lines + ' lines'
|
|
|
|
} else if (selectionCharCount > 0) {
|
|
|
|
selectionText += selectionCharCount + ' columns'
|
|
|
|
}
|
|
|
|
|
|
|
|
if (start.line !== end.line || selectionCharCount > 0) {
|
|
|
|
editorInstance.statusSelection.text(selectionText)
|
|
|
|
} else {
|
|
|
|
editorInstance.statusSelection.text('')
|
|
|
|
}
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
})
|
|
|
|
|
2017-03-27 23:18:36 -04:00
|
|
|
editorInstance.on('blur', function (cm) {
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < onlineUsers.length; i++) {
|
2017-04-11 00:07:04 -04:00
|
|
|
if (onlineUsers[i].id === personalInfo.id) {
|
|
|
|
onlineUsers[i].cursor = null
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
personalInfo.cursor = null
|
2017-03-08 13:41:05 -05:00
|
|
|
socket.emit('cursor blur')
|
|
|
|
})
|
2015-05-04 03:53:29 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function saveInfo () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const scrollbarStyle = editor.getOption('scrollbarStyle')
|
|
|
|
const left = $(window).scrollLeft()
|
|
|
|
const top = $(window).scrollTop()
|
2017-04-11 21:21:13 -04:00
|
|
|
switch (appState.currentMode) {
|
2017-03-08 13:41:05 -05:00
|
|
|
case modeType.edit:
|
|
|
|
if (scrollbarStyle === 'native') {
|
2017-04-11 00:07:04 -04:00
|
|
|
lastInfo.edit.scroll.left = left
|
|
|
|
lastInfo.edit.scroll.top = top
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
2017-04-11 00:07:04 -04:00
|
|
|
lastInfo.edit.scroll = editor.getScrollInfo()
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
break
|
|
|
|
case modeType.view:
|
2017-04-11 00:07:04 -04:00
|
|
|
lastInfo.view.scroll.left = left
|
|
|
|
lastInfo.view.scroll.top = top
|
2017-03-08 13:41:05 -05:00
|
|
|
break
|
|
|
|
case modeType.both:
|
2017-04-11 00:07:04 -04:00
|
|
|
lastInfo.edit.scroll = editor.getScrollInfo()
|
|
|
|
lastInfo.view.scroll.left = ui.area.view.scrollLeft()
|
|
|
|
lastInfo.view.scroll.top = ui.area.view.scrollTop()
|
2017-03-08 13:41:05 -05:00
|
|
|
break
|
|
|
|
}
|
2017-04-11 00:07:04 -04:00
|
|
|
lastInfo.edit.cursor = editor.getCursor()
|
|
|
|
lastInfo.edit.selections = editor.listSelections()
|
|
|
|
lastInfo.needRestore = true
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
function restoreInfo () {
|
2021-02-15 03:42:51 -05:00
|
|
|
const scrollbarStyle = editor.getOption('scrollbarStyle')
|
2017-04-11 00:07:04 -04:00
|
|
|
if (lastInfo.needRestore) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const line = lastInfo.edit.cursor.line
|
|
|
|
const ch = lastInfo.edit.cursor.ch
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.setCursor(line, ch)
|
2017-04-11 00:07:04 -04:00
|
|
|
editor.setSelections(lastInfo.edit.selections)
|
2017-04-11 21:21:13 -04:00
|
|
|
switch (appState.currentMode) {
|
2017-03-08 13:41:05 -05:00
|
|
|
case modeType.edit:
|
|
|
|
if (scrollbarStyle === 'native') {
|
2017-04-11 00:07:04 -04:00
|
|
|
$(window).scrollLeft(lastInfo.edit.scroll.left)
|
|
|
|
$(window).scrollTop(lastInfo.edit.scroll.top)
|
2017-03-08 13:41:05 -05:00
|
|
|
} else {
|
2021-02-15 03:42:51 -05:00
|
|
|
const left = lastInfo.edit.scroll.left
|
|
|
|
const top = lastInfo.edit.scroll.top
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.scrollIntoView()
|
|
|
|
editor.scrollTo(left, top)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case modeType.view:
|
2017-04-11 00:07:04 -04:00
|
|
|
$(window).scrollLeft(lastInfo.view.scroll.left)
|
|
|
|
$(window).scrollTop(lastInfo.view.scroll.top)
|
2017-03-08 13:41:05 -05:00
|
|
|
break
|
|
|
|
case modeType.both:
|
|
|
|
editor.scrollIntoView()
|
2021-02-15 03:42:51 -05:00
|
|
|
editor.scrollTo(lastInfo.edit.scroll.left, lastInfo.edit.scroll.top)
|
2017-04-11 00:07:04 -04:00
|
|
|
ui.area.view.scrollLeft(lastInfo.view.scroll.left)
|
|
|
|
ui.area.view.scrollTop(lastInfo.view.scroll.top)
|
2017-03-08 13:41:05 -05:00
|
|
|
break
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
|
2017-04-11 00:07:04 -04:00
|
|
|
lastInfo.needRestore = false
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
// view actions
|
|
|
|
function refreshView () {
|
|
|
|
ui.area.markdown.html('')
|
2017-04-10 23:48:39 -04:00
|
|
|
isDirty = true
|
2017-03-08 13:41:05 -05:00
|
|
|
updateViewInner()
|
2015-05-04 03:53:29 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const updateView = _.debounce(function () {
|
2017-03-08 13:41:05 -05:00
|
|
|
editor.operation(updateViewInner)
|
|
|
|
}, updateViewDebounce)
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let lastResult = null
|
|
|
|
let postUpdateEvent = null
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
function updateViewInner () {
|
2017-04-11 21:21:13 -04:00
|
|
|
if (appState.currentMode === modeType.edit || !isDirty) return
|
2021-02-15 03:42:51 -05:00
|
|
|
const value = editor.getValue()
|
|
|
|
const lastMeta = md.meta
|
2017-03-08 13:41:05 -05:00
|
|
|
md.meta = {}
|
|
|
|
delete md.metaError
|
2021-02-15 03:42:51 -05:00
|
|
|
let rendered = md.render(value)
|
2017-03-08 13:41:05 -05:00
|
|
|
if (md.meta.type && md.meta.type === 'slide') {
|
2019-05-30 04:53:08 -04:00
|
|
|
ui.area.view.addClass('black')
|
2021-02-15 03:42:51 -05:00
|
|
|
const slideOptions = {
|
2017-03-08 13:41:05 -05:00
|
|
|
separator: '^(\r\n?|\n)---(\r\n?|\n)$',
|
|
|
|
verticalSeparator: '^(\r\n?|\n)----(\r\n?|\n)$'
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const slides = window.RevealMarkdown.slidify(
|
|
|
|
editor.getValue(),
|
|
|
|
slideOptions
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.area.markdown.html(slides)
|
|
|
|
window.RevealMarkdown.initialize()
|
2019-05-30 18:27:56 -04:00
|
|
|
// prevent XSS
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.area.markdown.html(preventXSS(ui.area.markdown.html()))
|
|
|
|
ui.area.markdown.addClass('slides')
|
2017-04-11 21:21:13 -04:00
|
|
|
appState.syncscroll = false
|
2017-03-08 13:41:05 -05:00
|
|
|
checkSyncToggle()
|
|
|
|
} else {
|
|
|
|
if (lastMeta.type && lastMeta.type === 'slide') {
|
|
|
|
refreshView()
|
|
|
|
ui.area.markdown.removeClass('slides')
|
2019-05-30 04:53:08 -04:00
|
|
|
ui.area.view.removeClass('black')
|
2017-04-11 21:21:13 -04:00
|
|
|
appState.syncscroll = true
|
2017-03-08 13:41:05 -05:00
|
|
|
checkSyncToggle()
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// only render again when meta changed
|
2017-03-08 13:41:05 -05:00
|
|
|
if (JSON.stringify(md.meta) !== JSON.stringify(lastMeta)) {
|
2021-02-15 03:42:51 -05:00
|
|
|
parseMeta(
|
|
|
|
md,
|
|
|
|
ui.area.codemirror,
|
|
|
|
ui.area.markdown,
|
|
|
|
$('#ui-toc'),
|
|
|
|
$('#ui-toc-affix')
|
|
|
|
)
|
2017-03-08 13:41:05 -05:00
|
|
|
rendered = md.render(value)
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// prevent XSS
|
2017-03-08 13:41:05 -05:00
|
|
|
rendered = preventXSS(rendered)
|
2021-02-15 03:42:51 -05:00
|
|
|
const result = postProcess(rendered).children().toArray()
|
2017-03-08 13:41:05 -05:00
|
|
|
partialUpdate(result, lastResult, ui.area.markdown.children().toArray())
|
2021-02-15 03:42:51 -05:00
|
|
|
if (result && lastResult && result.length !== lastResult.length) {
|
|
|
|
updateDataAttrs(result, ui.area.markdown.children().toArray())
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
lastResult = $(result).clone()
|
|
|
|
}
|
2017-03-14 04:27:55 -04:00
|
|
|
removeDOMEvents(ui.area.markdown)
|
2017-03-08 13:41:05 -05:00
|
|
|
finishView(ui.area.markdown)
|
|
|
|
autoLinkify(ui.area.markdown)
|
|
|
|
deduplicatedHeaderId(ui.area.markdown)
|
|
|
|
renderTOC(ui.area.markdown)
|
|
|
|
generateToc('ui-toc')
|
|
|
|
generateToc('ui-toc-affix')
|
2018-11-19 12:29:50 -05:00
|
|
|
autoLinkify(ui.area.markdown)
|
2017-03-08 13:41:05 -05:00
|
|
|
generateScrollspy()
|
|
|
|
updateScrollspy()
|
|
|
|
smoothHashScroll()
|
2017-04-10 23:48:39 -04:00
|
|
|
isDirty = false
|
2017-03-08 13:41:05 -05:00
|
|
|
clearMap()
|
2019-05-30 18:27:56 -04:00
|
|
|
// buildMap();
|
2017-03-08 13:41:05 -05:00
|
|
|
updateTitleReminder()
|
2021-02-15 03:42:51 -05:00
|
|
|
if (postUpdateEvent && typeof postUpdateEvent === 'function') {
|
|
|
|
postUpdateEvent()
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const updateHistoryDebounce = 600
|
2015-09-25 06:01:15 -04:00
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const updateHistory = _.debounce(updateHistoryInner, updateHistoryDebounce)
|
2015-09-25 06:01:15 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function updateHistoryInner () {
|
|
|
|
writeHistory(renderFilename(ui.area.markdown), renderTags(ui.area.markdown))
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateDataAttrs (src, des) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// sync data attr startline and endline
|
2021-02-15 03:42:51 -05:00
|
|
|
for (let i = 0; i < src.length; i++) {
|
2017-03-08 13:41:05 -05:00
|
|
|
copyAttribute(src[i], des[i], 'data-startline')
|
|
|
|
copyAttribute(src[i], des[i], 'data-endline')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function partialUpdate (src, tar, des) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
!src ||
|
|
|
|
src.length === 0 ||
|
|
|
|
!tar ||
|
|
|
|
tar.length === 0 ||
|
|
|
|
!des ||
|
|
|
|
des.length === 0
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
ui.area.markdown.html(src)
|
|
|
|
return
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (src.length === tar.length) {
|
|
|
|
// same length
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let i = 0; i < src.length; i++) {
|
|
|
|
copyAttribute(src[i], des[i], 'data-startline')
|
|
|
|
copyAttribute(src[i], des[i], 'data-endline')
|
2021-02-15 03:42:51 -05:00
|
|
|
const rawSrc = cloneAndRemoveDataAttr(src[i])
|
|
|
|
const rawTar = cloneAndRemoveDataAttr(tar[i])
|
2017-03-08 13:41:05 -05:00
|
|
|
if (rawSrc.outerHTML !== rawTar.outerHTML) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// console.debug(rawSrc);
|
|
|
|
// console.debug(rawTar);
|
2017-03-08 13:41:05 -05:00
|
|
|
$(des[i]).replaceWith(src[i])
|
|
|
|
}
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
} else {
|
|
|
|
// diff length
|
|
|
|
let start = 0
|
2017-03-08 13:41:05 -05:00
|
|
|
// find diff start position
|
|
|
|
for (let i = 0; i < tar.length; i++) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// copyAttribute(src[i], des[i], 'data-startline');
|
|
|
|
// copyAttribute(src[i], des[i], 'data-endline');
|
2021-02-15 03:42:51 -05:00
|
|
|
const rawSrc = cloneAndRemoveDataAttr(src[i])
|
|
|
|
const rawTar = cloneAndRemoveDataAttr(tar[i])
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
|
|
|
|
start = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// find diff end position
|
2021-02-15 03:42:51 -05:00
|
|
|
let srcEnd = 0
|
|
|
|
let tarEnd = 0
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let i = 0; i < src.length; i++) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// copyAttribute(src[i], des[i], 'data-startline');
|
|
|
|
// copyAttribute(src[i], des[i], 'data-endline');
|
2021-02-15 03:42:51 -05:00
|
|
|
const rawSrc = cloneAndRemoveDataAttr(src[i])
|
|
|
|
const rawTar = cloneAndRemoveDataAttr(tar[i])
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
|
|
|
|
start = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// tar end
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let i = 1; i <= tar.length + 1; i++) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const srcLength = src.length
|
|
|
|
const tarLength = tar.length
|
2019-05-30 18:27:56 -04:00
|
|
|
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-startline');
|
|
|
|
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-endline');
|
2021-02-15 03:42:51 -05:00
|
|
|
const rawSrc = cloneAndRemoveDataAttr(src[srcLength - i])
|
|
|
|
const rawTar = cloneAndRemoveDataAttr(tar[tarLength - i])
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
|
|
|
|
tarEnd = tar.length - i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// src end
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let i = 1; i <= src.length + 1; i++) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const srcLength = src.length
|
|
|
|
const tarLength = tar.length
|
2019-05-30 18:27:56 -04:00
|
|
|
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-startline');
|
|
|
|
// copyAttribute(src[srcLength - i], des[srcLength - i], 'data-endline');
|
2021-02-15 03:42:51 -05:00
|
|
|
const rawSrc = cloneAndRemoveDataAttr(src[srcLength - i])
|
|
|
|
const rawTar = cloneAndRemoveDataAttr(tar[tarLength - i])
|
2017-03-08 13:41:05 -05:00
|
|
|
if (!rawSrc || !rawTar || rawSrc.outerHTML !== rawTar.outerHTML) {
|
|
|
|
srcEnd = src.length - i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// check if tar end overlap tar start
|
2021-02-15 03:42:51 -05:00
|
|
|
let overlap = 0
|
|
|
|
for (let i = start; i >= 0; i--) {
|
|
|
|
const rawTarStart = cloneAndRemoveDataAttr(tar[i - 1])
|
|
|
|
const rawTarEnd = cloneAndRemoveDataAttr(tar[tarEnd + 1 + start - i])
|
|
|
|
if (
|
|
|
|
rawTarStart &&
|
|
|
|
rawTarEnd &&
|
|
|
|
rawTarStart.outerHTML === rawTarEnd.outerHTML
|
|
|
|
) {
|
|
|
|
overlap++
|
|
|
|
} else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (debug) {
|
|
|
|
console.debug('overlap:' + overlap)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// show diff content
|
2017-03-08 13:41:05 -05:00
|
|
|
if (debug) {
|
2019-05-30 18:27:56 -04:00
|
|
|
console.debug('start:' + start)
|
|
|
|
console.debug('tarEnd:' + tarEnd)
|
|
|
|
console.debug('srcEnd:' + srcEnd)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
tarEnd += overlap
|
|
|
|
srcEnd += overlap
|
2021-02-15 03:42:51 -05:00
|
|
|
const repeatAdd = start - srcEnd < start - tarEnd
|
|
|
|
const repeatDiff = Math.abs(srcEnd - tarEnd) - 1
|
2019-05-30 18:27:56 -04:00
|
|
|
// push new elements
|
2021-02-15 03:42:51 -05:00
|
|
|
const newElements = []
|
2017-03-08 13:41:05 -05:00
|
|
|
if (srcEnd >= start) {
|
|
|
|
for (let j = start; j <= srcEnd; j++) {
|
|
|
|
if (!src[j]) continue
|
|
|
|
newElements.push(src[j].outerHTML)
|
|
|
|
}
|
|
|
|
} else if (repeatAdd) {
|
|
|
|
for (let j = srcEnd - repeatDiff; j <= srcEnd; j++) {
|
|
|
|
if (!des[j]) continue
|
|
|
|
newElements.push(des[j].outerHTML)
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// push remove elements
|
2021-02-15 03:42:51 -05:00
|
|
|
const removeElements = []
|
2017-03-08 13:41:05 -05:00
|
|
|
if (tarEnd >= start) {
|
|
|
|
for (let j = start; j <= tarEnd; j++) {
|
|
|
|
if (!des[j]) continue
|
|
|
|
removeElements.push(des[j])
|
|
|
|
}
|
|
|
|
} else if (!repeatAdd) {
|
|
|
|
for (let j = start; j <= start + repeatDiff; j++) {
|
|
|
|
if (!des[j]) continue
|
|
|
|
removeElements.push(des[j])
|
|
|
|
}
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// add elements
|
2017-03-08 13:41:05 -05:00
|
|
|
if (debug) {
|
2019-05-30 18:27:56 -04:00
|
|
|
console.debug('ADD ELEMENTS')
|
|
|
|
console.debug(newElements.join('\n'))
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (des[start]) {
|
|
|
|
$(newElements.join('')).insertBefore(des[start])
|
|
|
|
} else {
|
|
|
|
$(newElements.join('')).insertAfter(des[start - 1])
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// remove elements
|
2021-02-15 03:42:51 -05:00
|
|
|
if (debug) {
|
|
|
|
console.debug('REMOVE ELEMENTS')
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
for (let j = 0; j < removeElements.length; j++) {
|
|
|
|
if (debug) {
|
2019-05-30 18:27:56 -04:00
|
|
|
console.debug(removeElements[j].outerHTML)
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (removeElements[j]) {
|
|
|
|
$(removeElements[j]).remove()
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function cloneAndRemoveDataAttr (el) {
|
|
|
|
if (!el) return
|
2021-02-15 03:42:51 -05:00
|
|
|
const rawEl = $(el).clone()
|
2017-03-08 13:41:05 -05:00
|
|
|
rawEl.removeAttr('data-startline data-endline')
|
|
|
|
rawEl.find('[data-startline]').removeAttr('data-startline data-endline')
|
|
|
|
return rawEl[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
function copyAttribute (src, des, attr) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (src && src.getAttribute(attr) && des) {
|
|
|
|
des.setAttribute(attr, src.getAttribute(attr))
|
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($('.cursor-menu').length <= 0) {
|
2017-03-08 13:41:05 -05:00
|
|
|
$("<div class='cursor-menu'>").insertAfter('.CodeMirror-cursors')
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function reverseSortCursorMenu (dropdown) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const items = dropdown.find('.textcomplete-item')
|
2017-03-08 13:41:05 -05:00
|
|
|
items.sort(function (a, b) {
|
|
|
|
return $(b).attr('data-index') - $(a).attr('data-index')
|
|
|
|
})
|
|
|
|
return items
|
2016-03-14 23:12:45 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
const checkCursorMenu = _.throttle(checkCursorMenuInner, cursorMenuThrottle)
|
2016-02-25 00:45:02 -05:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkCursorMenuInner () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// get element
|
2021-02-15 03:42:51 -05:00
|
|
|
const dropdown = $('.cursor-menu > .dropdown-menu')
|
2019-05-30 18:27:56 -04:00
|
|
|
// return if not exists
|
2017-03-08 13:41:05 -05:00
|
|
|
if (dropdown.length <= 0) return
|
2019-05-30 18:27:56 -04:00
|
|
|
// set margin
|
2021-02-15 03:42:51 -05:00
|
|
|
const menuRightMargin = 10
|
|
|
|
const menuBottomMargin = 4
|
2019-05-30 18:27:56 -04:00
|
|
|
// use sizer to get the real doc size (won't count status bar and gutters)
|
2021-02-15 03:42:51 -05:00
|
|
|
const docWidth = ui.area.codemirrorSizer.width()
|
2019-05-30 18:27:56 -04:00
|
|
|
// get editor size (status bar not count in)
|
2021-02-15 03:42:51 -05:00
|
|
|
const editorHeight = ui.area.codemirror.height()
|
2019-05-30 18:27:56 -04:00
|
|
|
// get element size
|
2021-02-15 03:42:51 -05:00
|
|
|
const width = dropdown.outerWidth()
|
|
|
|
const height = dropdown.outerHeight()
|
2019-05-30 18:27:56 -04:00
|
|
|
// get cursor
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursor = editor.getCursor()
|
2019-05-30 18:27:56 -04:00
|
|
|
// set element cursor data
|
2021-02-15 03:42:51 -05:00
|
|
|
if (!dropdown.hasClass('CodeMirror-other-cursor')) {
|
|
|
|
dropdown.addClass('CodeMirror-other-cursor')
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
dropdown.attr('data-line', cursor.line)
|
|
|
|
dropdown.attr('data-ch', cursor.ch)
|
2019-05-30 18:27:56 -04:00
|
|
|
// get coord position
|
2021-02-15 03:42:51 -05:00
|
|
|
const coord = editor.charCoords(
|
|
|
|
{
|
|
|
|
line: cursor.line,
|
|
|
|
ch: cursor.ch
|
|
|
|
},
|
|
|
|
'windows'
|
|
|
|
)
|
|
|
|
const left = coord.left
|
|
|
|
const top = coord.top
|
2019-05-30 18:27:56 -04:00
|
|
|
// get doc top offset (to workaround with viewport)
|
2021-02-15 03:42:51 -05:00
|
|
|
const docTopOffset = ui.area.codemirrorSizerInner.position().top
|
2019-05-30 18:27:56 -04:00
|
|
|
// set offset
|
2021-02-15 03:42:51 -05:00
|
|
|
let offsetLeft = 0
|
|
|
|
let offsetTop = defaultTextHeight
|
2019-05-30 18:27:56 -04:00
|
|
|
// set up side down
|
2017-03-08 13:41:05 -05:00
|
|
|
window.upSideDown = false
|
2021-02-15 03:42:51 -05:00
|
|
|
let lastUpSideDown = (window.upSideDown = false)
|
2019-05-30 18:27:56 -04:00
|
|
|
// only do when have width and height
|
2017-03-08 13:41:05 -05:00
|
|
|
if (width > 0 && height > 0) {
|
2019-05-30 18:27:56 -04:00
|
|
|
// make element right bound not larger than doc width
|
2021-02-15 03:42:51 -05:00
|
|
|
if (left + width + offsetLeft + menuRightMargin > docWidth) {
|
|
|
|
offsetLeft = -(left + width - docWidth + menuRightMargin)
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// flip y when element bottom bound larger than doc height
|
|
|
|
// and element top position is larger than element height
|
2021-02-15 03:42:51 -05:00
|
|
|
if (
|
|
|
|
top + docTopOffset + height + offsetTop + menuBottomMargin >
|
|
|
|
Math.max(editor.doc.height, editorHeight) &&
|
|
|
|
top + docTopOffset > height + menuBottomMargin
|
|
|
|
) {
|
2017-03-08 13:41:05 -05:00
|
|
|
offsetTop = -(height + menuBottomMargin)
|
2019-05-30 18:27:56 -04:00
|
|
|
// reverse sort menu because upSideDown
|
2017-03-08 13:41:05 -05:00
|
|
|
dropdown.html(reverseSortCursorMenu(dropdown))
|
|
|
|
window.upSideDown = true
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
const textCompleteDropdown = $(editor.getInputField()).data('textComplete')
|
|
|
|
.dropdown
|
2017-03-08 13:41:05 -05:00
|
|
|
lastUpSideDown = textCompleteDropdown.upSideDown
|
|
|
|
textCompleteDropdown.upSideDown = window.upSideDown
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// make menu scroll top only if upSideDown changed
|
2021-02-15 03:42:51 -05:00
|
|
|
if (window.upSideDown !== lastUpSideDown) {
|
|
|
|
dropdown.scrollTop(dropdown[0].scrollHeight)
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
// set element offset data
|
2017-03-08 13:41:05 -05:00
|
|
|
dropdown.attr('data-offset-left', offsetLeft)
|
|
|
|
dropdown.attr('data-offset-top', offsetTop)
|
2019-05-30 18:27:56 -04:00
|
|
|
// set position
|
2017-03-08 13:41:05 -05:00
|
|
|
dropdown[0].style.left = left + offsetLeft + 'px'
|
|
|
|
dropdown[0].style.top = top + offsetTop + 'px'
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkInIndentCode () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// if line starts with tab or four spaces is a code block
|
2021-02-15 03:42:51 -05:00
|
|
|
const line = editor.getLine(editor.getCursor().line)
|
|
|
|
const isIndentCode =
|
|
|
|
line.substr(0, 4) === ' ' || line.substr(0, 1) === '\t'
|
2017-03-08 13:41:05 -05:00
|
|
|
return isIndentCode
|
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let isInCode = false
|
2017-03-08 13:41:05 -05:00
|
|
|
|
|
|
|
function checkInCode () {
|
|
|
|
isInCode = checkAbove(matchInCode) || checkInIndentCode()
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkAbove (method) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursor = editor.getCursor()
|
|
|
|
let text = []
|
|
|
|
for (let i = 0; i < cursor.line; i++) {
|
|
|
|
// contain current line
|
2017-03-08 13:41:05 -05:00
|
|
|
text.push(editor.getLine(i))
|
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
text =
|
|
|
|
text.join('\n') + '\n' + editor.getLine(cursor.line).slice(0, cursor.ch)
|
2019-05-30 18:27:56 -04:00
|
|
|
// console.debug(text);
|
2017-03-08 13:41:05 -05:00
|
|
|
return method(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkBelow (method) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const cursor = editor.getCursor()
|
|
|
|
const count = editor.lineCount()
|
|
|
|
let text = []
|
|
|
|
for (let i = cursor.line + 1; i < count; i++) {
|
|
|
|
// contain current line
|
2017-03-08 13:41:05 -05:00
|
|
|
text.push(editor.getLine(i))
|
|
|
|
}
|
|
|
|
text = editor.getLine(cursor.line).slice(cursor.ch) + '\n' + text.join('\n')
|
2019-05-30 18:27:56 -04:00
|
|
|
// console.debug(text);
|
2017-03-08 13:41:05 -05:00
|
|
|
return method(text)
|
|
|
|
}
|
|
|
|
|
|
|
|
function matchInCode (text) {
|
2021-02-15 03:42:51 -05:00
|
|
|
let match
|
2017-03-08 13:41:05 -05:00
|
|
|
match = text.match(/`{3,}/g)
|
|
|
|
if (match && match.length % 2) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
match = text.match(/`/g)
|
2015-06-01 06:04:25 -04:00
|
|
|
if (match && match.length % 2) {
|
2017-03-08 13:41:05 -05:00
|
|
|
return true
|
2015-06-01 06:04:25 -04:00
|
|
|
} else {
|
2017-03-08 13:41:05 -05:00
|
|
|
return false
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2015-06-01 06:04:25 -04:00
|
|
|
}
|
|
|
|
|
2021-02-15 03:42:51 -05:00
|
|
|
let isInContainer = false
|
|
|
|
let isInContainerSyntax = false
|
2016-03-14 23:04:45 -04:00
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkInContainer () {
|
|
|
|
isInContainer = checkAbove(matchInContainer) && !checkInIndentCode()
|
2016-03-14 23:04:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function checkInContainerSyntax () {
|
2019-05-30 18:27:56 -04:00
|
|
|
// if line starts with :::, it's in container syntax
|
2021-02-15 03:42:51 -05:00
|
|
|
const line = editor.getLine(editor.getCursor().line)
|
|
|
|
isInContainerSyntax = line.substr(0, 3) === ':::'
|
2016-03-14 23:04:45 -04:00
|
|
|
}
|
|
|
|
|
2017-03-08 13:41:05 -05:00
|
|
|
function matchInContainer (text) {
|
2021-02-15 03:42:51 -05:00
|
|
|
const match = text.match(/:{3,}/g)
|
2017-03-08 13:41:05 -05:00
|
|
|
if (match && match.length % 2) {
|
|
|
|
return true
|
|
|
|
} else {
|
|
|
|
return false
|
|
|
|
}
|
2016-03-14 23:04:45 -04:00
|
|
|
}
|
|
|
|
|
2015-06-01 06:04:25 -04:00
|
|
|
$(editor.getInputField())
|
2021-02-15 03:42:51 -05:00
|
|
|
.textcomplete(
|
|
|
|
[
|
|
|
|
{
|
|
|
|
// emoji strategy
|
|
|
|
match: /(^|\n|\s)\B:([-+\w]*)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
const line = editor.getLine(editor.getCursor().line)
|
|
|
|
term = line.match(this.match)[2]
|
|
|
|
const list = []
|
|
|
|
$.map(window.emojify.emojiNames, function (emoji) {
|
|
|
|
if (emoji.indexOf(term) === 0) {
|
|
|
|
// match at first character
|
|
|
|
list.push(emoji)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
$.map(window.emojify.emojiNames, function (emoji) {
|
|
|
|
if (emoji.indexOf(term) !== -1) {
|
|
|
|
// match inside the word
|
|
|
|
list.push(emoji)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callback(list)
|
|
|
|
},
|
|
|
|
template: function (value) {
|
|
|
|
return (
|
|
|
|
'<img class="emoji" src="' +
|
|
|
|
serverurl +
|
|
|
|
'/build/emojify.js/dist/images/basic/' +
|
|
|
|
value +
|
|
|
|
'.png"></img> ' +
|
|
|
|
value
|
|
|
|
)
|
|
|
|
},
|
|
|
|
replace: function (value) {
|
|
|
|
return '$1:' + value + ': '
|
|
|
|
},
|
|
|
|
index: 1,
|
|
|
|
context: function (text) {
|
|
|
|
checkInCode()
|
|
|
|
checkInContainer()
|
|
|
|
checkInContainerSyntax()
|
|
|
|
return !isInCode && !isInContainerSyntax
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Code block language strategy
|
|
|
|
langs: supportCodeModes,
|
|
|
|
charts: supportCharts,
|
|
|
|
match: /(^|\n)```(\w+)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
const line = editor.getLine(editor.getCursor().line)
|
|
|
|
term = line.match(this.match)[2]
|
|
|
|
const list = []
|
|
|
|
$.map(this.langs, function (lang) {
|
|
|
|
if (lang.indexOf(term) === 0 && lang !== term) {
|
|
|
|
list.push(lang)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
$.map(this.charts, function (chart) {
|
|
|
|
if (chart.indexOf(term) === 0 && chart !== term) {
|
|
|
|
list.push(chart)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callback(list)
|
|
|
|
},
|
|
|
|
replace: function (lang) {
|
|
|
|
let ending = ''
|
|
|
|
if (!checkBelow(matchInCode)) {
|
|
|
|
ending = '\n\n```'
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
if (this.langs.indexOf(lang) !== -1) {
|
|
|
|
return '$1```' + lang + '=' + ending
|
|
|
|
} else if (this.charts.indexOf(lang) !== -1) {
|
|
|
|
return '$1```' + lang + ending
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
},
|
|
|
|
done: function () {
|
|
|
|
const cursor = editor.getCursor()
|
|
|
|
let text = []
|
|
|
|
text.push(editor.getLine(cursor.line - 1))
|
|
|
|
text.push(editor.getLine(cursor.line))
|
|
|
|
text = text.join('\n')
|
|
|
|
// console.debug(text);
|
|
|
|
if (text === '\n```') {
|
|
|
|
editor.doc.cm.execCommand('goLineUp')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
context: function (text) {
|
|
|
|
return isInCode
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
{
|
|
|
|
// Container strategy
|
|
|
|
containers: supportContainers,
|
|
|
|
match: /(^|\n):::(\s*)(\w*)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
const line = editor.getLine(editor.getCursor().line)
|
|
|
|
term = line.match(this.match)[3].trim()
|
|
|
|
const list = []
|
|
|
|
$.map(this.containers, function (container) {
|
|
|
|
if (container.indexOf(term) === 0 && container !== term) {
|
|
|
|
list.push(container)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callback(list)
|
|
|
|
},
|
|
|
|
replace: function (lang) {
|
|
|
|
let ending = ''
|
|
|
|
if (!checkBelow(matchInContainer)) {
|
|
|
|
ending = '\n\n:::'
|
|
|
|
}
|
|
|
|
if (this.containers.indexOf(lang) !== -1) {
|
|
|
|
return '$1:::$2' + lang + ending
|
|
|
|
}
|
|
|
|
},
|
|
|
|
done: function () {
|
|
|
|
const cursor = editor.getCursor()
|
|
|
|
let text = []
|
|
|
|
text.push(editor.getLine(cursor.line - 1))
|
|
|
|
text.push(editor.getLine(cursor.line))
|
|
|
|
text = text.join('\n')
|
|
|
|
// console.debug(text);
|
|
|
|
if (text === '\n:::') {
|
|
|
|
editor.doc.cm.execCommand('goLineUp')
|
|
|
|
}
|
|
|
|
},
|
|
|
|
context: function (text) {
|
|
|
|
return !isInCode && isInContainer
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
{
|
|
|
|
// header
|
|
|
|
match: /(?:^|\n)(\s{0,3})(#{1,6}\w*)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
callback(
|
|
|
|
$.map(supportHeaders, function (header) {
|
|
|
|
return header.search.indexOf(term) === 0 ? header.text : null
|
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
replace: function (value) {
|
|
|
|
return '$1' + value
|
|
|
|
},
|
|
|
|
context: function (text) {
|
|
|
|
return !isInCode
|
|
|
|
}
|
2019-05-30 18:27:56 -04:00
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
{
|
|
|
|
// extra tags for list
|
|
|
|
match: /(^[>\s]*[-+*]\s(?:\[[x ]\]|.*))(\[\])(\w*)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
const list = []
|
2017-03-23 08:49:31 -04:00
|
|
|
$.map(supportExtraTags, function (extratag) {
|
2021-02-15 03:42:51 -05:00
|
|
|
if (extratag.search.indexOf(term) === 0) {
|
|
|
|
list.push(extratag.command())
|
|
|
|
}
|
2017-03-23 08:49:31 -04:00
|
|
|
})
|
2021-02-15 03:42:51 -05:00
|
|
|
$.map(supportReferrals, function (referral) {
|
|
|
|
if (referral.search.indexOf(term) === 0) {
|
|
|
|
list.push(referral.text)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callback(list)
|
|
|
|
},
|
|
|
|
replace: function (value) {
|
|
|
|
return '$1' + value
|
|
|
|
},
|
|
|
|
context: function (text) {
|
|
|
|
return !isInCode
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
{
|
|
|
|
// extra tags for blockquote
|
|
|
|
match: /(?:^|\n|\s)(>.*|\s|)((\^|)\[(\^|)\](\[\]|\(\)|:|)\s*\w*)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
const line = editor.getLine(editor.getCursor().line)
|
|
|
|
const quote = line.match(this.match)[1].trim()
|
|
|
|
const list = []
|
|
|
|
if (quote.indexOf('>') === 0) {
|
|
|
|
$.map(supportExtraTags, function (extratag) {
|
|
|
|
if (extratag.search.indexOf(term) === 0) {
|
|
|
|
list.push(extratag.command())
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
$.map(supportReferrals, function (referral) {
|
|
|
|
if (referral.search.indexOf(term) === 0) {
|
|
|
|
list.push(referral.text)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
callback(list)
|
|
|
|
},
|
|
|
|
replace: function (value) {
|
|
|
|
return '$1' + value
|
|
|
|
},
|
|
|
|
context: function (text) {
|
|
|
|
return !isInCode
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
{
|
|
|
|
// referral
|
|
|
|
match: /(^\s*|\n|\s{2})((\[\]|\[\]\[\]|\[\]\(\)|!|!\[\]|!\[\]\[\]|!\[\]\(\))\s*\w*)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
callback(
|
|
|
|
$.map(supportReferrals, function (referral) {
|
|
|
|
return referral.search.indexOf(term) === 0 ? referral.text : null
|
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
replace: function (value) {
|
|
|
|
return '$1' + value
|
|
|
|
},
|
|
|
|
context: function (text) {
|
|
|
|
return !isInCode
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
{
|
|
|
|
// externals
|
|
|
|
match: /(^|\n|\s)\{\}(\w*)$/,
|
|
|
|
search: function (term, callback) {
|
|
|
|
callback(
|
|
|
|
$.map(supportExternals, function (external) {
|
|
|
|
return external.search.indexOf(term) === 0 ? external.text : null
|
|
|
|
})
|
|
|
|
)
|
|
|
|
},
|
|
|
|
replace: function (value) {
|
|
|
|
return '$1' + value
|
|
|
|
},
|
|
|
|
context: function (text) {
|
|
|
|
return !isInCode
|
|
|
|
}
|
2017-03-08 13:41:05 -05:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
],
|
|
|
|
{
|
|
|
|
appendTo: $('.cursor-menu')
|
2019-05-30 18:27:56 -04:00
|
|
|
}
|
2021-02-15 03:42:51 -05:00
|
|
|
)
|
2019-05-30 18:27:56 -04:00
|
|
|
.on({
|
|
|
|
'textComplete:beforeSearch': function (e) {
|
|
|
|
// NA
|
|
|
|
},
|
|
|
|
'textComplete:afterSearch': function (e) {
|
|
|
|
checkCursorMenu()
|
|
|
|
},
|
|
|
|
'textComplete:select': function (e, value, strategy) {
|
|
|
|
// NA
|
|
|
|
},
|
|
|
|
'textComplete:show': function (e) {
|
|
|
|
$(this).data('autocompleting', true)
|
|
|
|
editor.setOption('extraKeys', {
|
2021-02-15 03:42:51 -05:00
|
|
|
Up: function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
return false
|
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
Right: function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
editor.doc.cm.execCommand('goCharRight')
|
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
Down: function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
return false
|
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
Left: function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
editor.doc.cm.execCommand('goCharLeft')
|
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
Enter: function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
return false
|
|
|
|
},
|
2021-02-15 03:42:51 -05:00
|
|
|
Backspace: function () {
|
2019-05-30 18:27:56 -04:00
|
|
|
editor.doc.cm.execCommand('delCharBefore')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
|
|
|
'textComplete:hide': function (e) {
|
|
|
|
$(this).data('autocompleting', false)
|
|
|
|
editor.setOption('extraKeys', editorInstance.defaultExtraKeys)
|
|
|
|
}
|
|
|
|
})
|