2020-12-16 13:12:25 +01:00
|
|
|
'use strict';
|
2020-12-20 08:15:58 +01:00
|
|
|
|
2011-12-04 16:33:56 +01:00
|
|
|
/**
|
2013-03-09 23:57:42 +01:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 16:33:56 +01:00
|
|
|
* This helps other people to understand this code better and helps them to improve it.
|
|
|
|
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
|
|
|
|
*/
|
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 19:59:34 +02:00
|
|
|
*
|
2011-03-26 14:10:41 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS-IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const browser = require('./browser');
|
|
|
|
const hooks = require('./pluginfw/hooks');
|
|
|
|
const padutils = require('./pad_utils').padutils;
|
|
|
|
const padeditor = require('./pad_editor').padeditor;
|
|
|
|
const padsavedrevs = require('./pad_savedrevs');
|
2020-12-16 13:12:25 +01:00
|
|
|
const _ = require('underscore');
|
|
|
|
require('./vendors/nice-select');
|
2012-01-16 06:37:47 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
const ToolbarItem = function (element) {
|
2013-04-15 20:32:59 +02:00
|
|
|
this.$el = element;
|
|
|
|
};
|
|
|
|
|
|
|
|
ToolbarItem.prototype.getCommand = function () {
|
2020-11-23 19:24:19 +01:00
|
|
|
return this.$el.attr('data-key');
|
2013-04-15 20:32:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ToolbarItem.prototype.getValue = function () {
|
|
|
|
if (this.isSelect()) {
|
2020-11-23 19:24:19 +01:00
|
|
|
return this.$el.find('select').val();
|
2013-04-15 20:32:59 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-16 01:06:32 +02:00
|
|
|
ToolbarItem.prototype.setValue = function (val) {
|
|
|
|
if (this.isSelect()) {
|
2020-11-23 19:24:19 +01:00
|
|
|
return this.$el.find('select').val(val);
|
2013-04-16 01:06:32 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-04-15 20:32:59 +02:00
|
|
|
ToolbarItem.prototype.getType = function () {
|
2020-11-23 19:24:19 +01:00
|
|
|
return this.$el.attr('data-type');
|
2013-04-15 20:32:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ToolbarItem.prototype.isSelect = function () {
|
2020-12-16 13:12:25 +01:00
|
|
|
return this.getType() === 'select';
|
2013-04-15 20:32:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ToolbarItem.prototype.isButton = function () {
|
2020-12-16 13:12:25 +01:00
|
|
|
return this.getType() === 'button';
|
2013-04-15 20:32:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
ToolbarItem.prototype.bind = function (callback) {
|
2020-11-23 19:24:19 +01:00
|
|
|
const self = this;
|
2013-04-16 01:06:32 +02:00
|
|
|
|
2013-04-15 20:32:59 +02:00
|
|
|
if (self.isButton()) {
|
2020-11-23 19:24:19 +01:00
|
|
|
self.$el.click((event) => {
|
2015-03-25 16:49:41 +01:00
|
|
|
$(':focus').blur();
|
2013-04-16 01:06:32 +02:00
|
|
|
callback(self.getCommand(), self);
|
2013-04-15 20:32:59 +02:00
|
|
|
event.preventDefault();
|
|
|
|
});
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (self.isSelect()) {
|
|
|
|
self.$el.find('select').change(() => {
|
2013-04-16 01:06:32 +02:00
|
|
|
callback(self.getCommand(), self);
|
2013-04-15 20:32:59 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2020-12-16 13:12:25 +01:00
|
|
|
const padeditbar = (function () {
|
|
|
|
const syncAnimationFn = () => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const SYNCING = -100;
|
|
|
|
const DONE = 100;
|
|
|
|
let state = DONE;
|
|
|
|
const fps = 25;
|
|
|
|
const step = 1 / fps;
|
|
|
|
const T_START = -0.5;
|
|
|
|
const T_FADE = 1.0;
|
|
|
|
const T_GONE = 1.5;
|
|
|
|
const animator = padutils.makeAnimationScheduler(() => {
|
2020-12-16 13:12:25 +01:00
|
|
|
if (state === SYNCING || state === DONE) {
|
2011-03-26 14:10:41 +01:00
|
|
|
return false;
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (state >= T_GONE) {
|
2011-03-26 14:10:41 +01:00
|
|
|
state = DONE;
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#syncstatussyncing').css('display', 'none');
|
|
|
|
$('#syncstatusdone').css('display', 'none');
|
2011-03-26 14:10:41 +01:00
|
|
|
return false;
|
2020-11-23 19:24:19 +01:00
|
|
|
} else if (state < 0) {
|
2011-03-26 14:10:41 +01:00
|
|
|
state += step;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (state >= 0) {
|
|
|
|
$('#syncstatussyncing').css('display', 'none');
|
|
|
|
$('#syncstatusdone').css('display', 'block').css('opacity', 1);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return true;
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2011-03-26 14:10:41 +01:00
|
|
|
state += step;
|
2020-11-23 19:24:19 +01:00
|
|
|
if (state >= T_FADE) {
|
|
|
|
$('#syncstatusdone').css('opacity', (T_GONE - state) / (T_GONE - T_FADE));
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2011-07-07 19:59:34 +02:00
|
|
|
}, step * 1000);
|
2011-03-26 14:10:41 +01:00
|
|
|
return {
|
2020-12-16 13:12:25 +01:00
|
|
|
syncing: () => {
|
2011-03-26 14:10:41 +01:00
|
|
|
state = SYNCING;
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#syncstatussyncing').css('display', 'block');
|
|
|
|
$('#syncstatusdone').css('display', 'none');
|
2011-03-26 14:10:41 +01:00
|
|
|
},
|
2020-12-16 13:12:25 +01:00
|
|
|
done: () => {
|
2011-03-26 14:10:41 +01:00
|
|
|
state = T_START;
|
|
|
|
animator.scheduleAnimation();
|
2020-11-23 19:24:19 +01:00
|
|
|
},
|
2011-03-26 14:10:41 +01:00
|
|
|
};
|
2020-12-16 13:12:25 +01:00
|
|
|
};
|
|
|
|
const syncAnimation = syncAnimationFn();
|
2011-03-26 14:10:41 +01:00
|
|
|
|
2020-12-16 13:12:25 +01:00
|
|
|
const self = {
|
2020-11-23 19:24:19 +01:00
|
|
|
init() {
|
|
|
|
const self = this;
|
2014-03-16 14:04:12 +01:00
|
|
|
self.dropdowns = [];
|
2014-11-06 16:43:21 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#editbar .editbarbutton').attr('unselectable', 'on'); // for IE
|
2020-09-10 19:11:10 +02:00
|
|
|
this.enable();
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#editbar [data-key]').each(function () {
|
|
|
|
$(this).unbind('click');
|
|
|
|
(new ToolbarItem($(this))).bind((command, item) => {
|
2013-04-16 01:06:32 +02:00
|
|
|
self.triggerCommand(command, item);
|
2012-02-29 23:13:02 +01:00
|
|
|
});
|
|
|
|
});
|
2013-04-15 20:32:59 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
$('body:not(#editorcontainerbox)').on('keydown', (evt) => {
|
2015-03-31 14:45:11 +02:00
|
|
|
bodyKeyEvent(evt);
|
2015-03-25 12:03:45 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
$('.show-more-icon-btn').click(() => {
|
2020-04-04 11:08:05 +02:00
|
|
|
$('.toolbar').toggleClass('full-icons');
|
|
|
|
});
|
|
|
|
self.checkAllIconsAreDisplayedInToolbar();
|
2020-11-23 19:24:19 +01:00
|
|
|
$(window).resize(_.debounce(self.checkAllIconsAreDisplayedInToolbar, 100));
|
2020-04-04 11:08:05 +02:00
|
|
|
|
2013-04-15 20:32:59 +02:00
|
|
|
registerDefaultCommands(self);
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
hooks.callAll('postToolbarInit', {
|
2013-04-15 20:32:59 +02:00
|
|
|
toolbar: self,
|
2020-11-23 19:24:19 +01:00
|
|
|
ace: padeditor.ace,
|
2013-04-15 20:32:59 +02:00
|
|
|
});
|
2020-04-14 09:27:13 +02:00
|
|
|
|
2020-04-28 15:25:03 +02:00
|
|
|
/*
|
|
|
|
* On safari, the dropdown in the toolbar gets hidden because of toolbar
|
|
|
|
* overflow:hidden property. This is a bug from Safari: any children with
|
|
|
|
* position:fixed (like the dropdown) should be displayed no matter
|
|
|
|
* overflow:hidden on parent
|
|
|
|
*/
|
|
|
|
if (!browser.safari) {
|
2020-11-23 19:24:19 +01:00
|
|
|
$('select').niceSelect();
|
2020-04-28 15:25:03 +02:00
|
|
|
}
|
2020-04-17 08:44:56 +02:00
|
|
|
|
|
|
|
// When editor is scrolled, we add a class to style the editbar differently
|
2020-11-23 19:24:19 +01:00
|
|
|
$('iframe[name="ace_outer"]').contents().scroll(function () {
|
2020-04-17 08:44:56 +02:00
|
|
|
$('#editbar').toggleClass('editor-scrolled', $(this).scrollTop() > 2);
|
2020-11-23 19:24:19 +01:00
|
|
|
});
|
2011-03-26 14:10:41 +01:00
|
|
|
},
|
2020-12-16 13:12:25 +01:00
|
|
|
isEnabled: () => true,
|
|
|
|
disable: () => {
|
2020-11-23 19:24:19 +01:00
|
|
|
$('#editbar').addClass('disabledtoolbar').removeClass('enabledtoolbar');
|
2011-03-26 14:10:41 +01:00
|
|
|
},
|
2020-12-16 13:12:25 +01:00
|
|
|
enable: () => {
|
2020-09-10 19:11:10 +02:00
|
|
|
$('#editbar').addClass('enabledtoolbar').removeClass('disabledtoolbar');
|
|
|
|
},
|
2013-03-10 23:42:12 +01:00
|
|
|
commands: {},
|
2020-11-23 19:24:19 +01:00
|
|
|
registerCommand(cmd, callback) {
|
2013-03-10 23:42:12 +01:00
|
|
|
this.commands[cmd] = callback;
|
|
|
|
return this;
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
registerDropdownCommand(cmd, dropdown) {
|
2013-03-10 23:42:12 +01:00
|
|
|
dropdown = dropdown || cmd;
|
2020-11-23 19:24:19 +01:00
|
|
|
self.dropdowns.push(dropdown);
|
|
|
|
this.registerCommand(cmd, () => {
|
2013-03-10 23:42:12 +01:00
|
|
|
self.toggleDropDown(dropdown);
|
|
|
|
});
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
registerAceCommand(cmd, callback) {
|
|
|
|
this.registerCommand(cmd, (cmd, ace, item) => {
|
|
|
|
ace.callWithAce((ace) => {
|
2016-03-18 20:11:29 +01:00
|
|
|
callback(cmd, ace, item);
|
2013-03-10 23:42:12 +01:00
|
|
|
}, cmd, true);
|
|
|
|
});
|
|
|
|
},
|
2020-11-23 19:24:19 +01:00
|
|
|
triggerCommand(cmd, item) {
|
2013-04-15 20:32:59 +02:00
|
|
|
if (self.isEnabled() && this.commands[cmd]) {
|
2013-04-16 01:06:32 +02:00
|
|
|
this.commands[cmd](cmd, padeditor.ace, item);
|
2011-03-26 14:10:41 +01:00
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
if (padeditor.ace) padeditor.ace.focus();
|
2011-03-26 14:10:41 +01:00
|
|
|
},
|
2020-12-16 13:12:25 +01:00
|
|
|
toggleDropDown: (moduleName, cb) => {
|
2020-04-02 14:36:49 +02:00
|
|
|
// do nothing if users are sticked
|
2020-11-23 19:24:19 +01:00
|
|
|
if (moduleName === 'users' && $('#users').hasClass('stickyUsers')) {
|
2020-04-02 14:36:49 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-21 23:46:54 +02:00
|
|
|
$('.nice-select').removeClass('open');
|
2020-11-23 19:24:19 +01:00
|
|
|
$('.toolbar-popup').removeClass('popup-show');
|
2020-04-21 23:46:54 +02:00
|
|
|
|
2012-04-29 01:00:31 +02:00
|
|
|
// hide all modules and remove highlighting of all buttons
|
2020-12-16 13:12:25 +01:00
|
|
|
if (moduleName === 'none') {
|
2020-11-23 19:24:19 +01:00
|
|
|
const returned = false;
|
2020-12-16 13:12:25 +01:00
|
|
|
for (let i = 0; i < self.dropdowns.length; i++) {
|
|
|
|
const thisModuleName = self.dropdowns[i];
|
2017-05-04 19:34:01 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
// skip the userlist
|
2020-12-16 13:12:25 +01:00
|
|
|
if (thisModuleName === 'users') continue;
|
2013-03-09 23:57:42 +01:00
|
|
|
|
2020-12-16 13:12:25 +01:00
|
|
|
const module = $(`#${thisModuleName}`);
|
2017-05-04 19:34:01 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
// skip any "force reconnect" message
|
|
|
|
const isAForceReconnectMessage = module.find('button#forcereconnect:visible').length > 0;
|
|
|
|
if (isAForceReconnectMessage) continue;
|
2020-04-21 23:46:54 +02:00
|
|
|
if (module.hasClass('popup-show')) {
|
2020-11-23 19:24:19 +01:00
|
|
|
$(`li[data-key=${thisModuleName}] > a`).removeClass('selected');
|
|
|
|
module.removeClass('popup-show');
|
2011-07-08 16:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-21 23:46:54 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (!returned && cb) return cb();
|
|
|
|
} else {
|
2012-04-29 01:00:31 +02:00
|
|
|
// hide all modules that are not selected and remove highlighting
|
|
|
|
// respectively add highlighting to the corresponding button
|
2020-12-16 13:12:25 +01:00
|
|
|
for (let i = 0; i < self.dropdowns.length; i++) {
|
|
|
|
const thisModuleName = self.dropdowns[i];
|
|
|
|
const module = $(`#${thisModuleName}`);
|
2013-03-09 23:57:42 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if (module.hasClass('popup-show')) {
|
|
|
|
$(`li[data-key=${thisModuleName}] > a`).removeClass('selected');
|
|
|
|
module.removeClass('popup-show');
|
2020-12-16 13:12:25 +01:00
|
|
|
} else if (thisModuleName === moduleName) {
|
2020-11-23 19:24:19 +01:00
|
|
|
$(`li[data-key=${thisModuleName}] > a`).addClass('selected');
|
|
|
|
module.addClass('popup-show');
|
2020-04-06 19:30:51 +02:00
|
|
|
if (cb) {
|
|
|
|
cb();
|
|
|
|
}
|
2011-07-08 16:19:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-12-16 13:12:25 +01:00
|
|
|
setSyncStatus: (status) => {
|
|
|
|
if (status === 'syncing') {
|
2011-03-26 14:10:41 +01:00
|
|
|
syncAnimation.syncing();
|
2020-12-16 13:12:25 +01:00
|
|
|
} else if (status === 'done') {
|
2011-03-26 14:10:41 +01:00
|
|
|
syncAnimation.done();
|
|
|
|
}
|
2011-11-24 16:34:28 +01:00
|
|
|
},
|
2020-12-16 13:12:25 +01:00
|
|
|
setEmbedLinks: () => {
|
2020-11-23 19:24:19 +01:00
|
|
|
const padUrl = window.location.href.split('?')[0];
|
2020-12-16 13:12:25 +01:00
|
|
|
const params = '?showControls=true&showChat=true&showLineNumbers=true&useMonospaceFont=false';
|
|
|
|
const props = 'width="100%" height="600" frameborder="0"';
|
2020-06-01 23:12:42 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
if ($('#readonlyinput').is(':checked')) {
|
|
|
|
const urlParts = padUrl.split('/');
|
2020-06-01 23:12:42 +02:00
|
|
|
urlParts.pop();
|
2020-11-23 19:24:19 +01:00
|
|
|
const readonlyLink = `${urlParts.join('/')}/${clientVars.readOnlyId}`;
|
2020-12-16 13:12:25 +01:00
|
|
|
$('#embedinput')
|
|
|
|
.val(`<iframe name="embed_readonly" src="${readonlyLink}${params}" ${props}></iframe>`);
|
2011-11-24 16:34:28 +01:00
|
|
|
$('#linkinput').val(readonlyLink);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2020-12-16 13:12:25 +01:00
|
|
|
$('#embedinput')
|
|
|
|
.val(`<iframe name="embed_readwrite" src="${padUrl}${params}" ${props}></iframe>`);
|
2020-06-01 23:12:42 +02:00
|
|
|
$('#linkinput').val(padUrl);
|
2011-11-24 16:34:28 +01:00
|
|
|
}
|
2020-04-04 11:08:05 +02:00
|
|
|
},
|
2020-12-16 13:12:25 +01:00
|
|
|
checkAllIconsAreDisplayedInToolbar: () => {
|
2020-04-04 11:08:05 +02:00
|
|
|
// reset style
|
2020-11-23 19:24:19 +01:00
|
|
|
$('.toolbar').removeClass('cropped');
|
2020-09-19 20:09:30 +02:00
|
|
|
$('body').removeClass('mobile-layout');
|
2020-11-23 19:24:19 +01:00
|
|
|
const menu_left = $('.toolbar .menu_left')[0];
|
2020-04-29 13:04:05 +02:00
|
|
|
|
2020-12-16 13:12:25 +01:00
|
|
|
// this is approximate, we cannot measure it because on mobile
|
|
|
|
// Layout it takes the full width on the bottom of the page
|
|
|
|
const menuRightWidth = 280;
|
|
|
|
if (menu_left && menu_left.scrollWidth > $('.toolbar').width() - menuRightWidth ||
|
|
|
|
$('.toolbar').width() < 1000) {
|
2020-09-19 20:09:30 +02:00
|
|
|
$('body').addClass('mobile-layout');
|
|
|
|
}
|
|
|
|
if (menu_left && menu_left.scrollWidth > $('.toolbar').width()) {
|
2020-04-04 11:08:05 +02:00
|
|
|
$('.toolbar').addClass('cropped');
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
},
|
2011-03-26 14:10:41 +01:00
|
|
|
};
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
let editbarPosition = 0;
|
2015-03-31 15:47:00 +02:00
|
|
|
|
2020-12-16 13:12:25 +01:00
|
|
|
const bodyKeyEvent = (evt) => {
|
2015-03-26 15:26:21 +01:00
|
|
|
// If the event is Alt F9 or Escape & we're already in the editbar menu
|
2015-03-25 16:38:19 +01:00
|
|
|
// Send the users focus back to the pad
|
2020-11-23 19:24:19 +01:00
|
|
|
if ((evt.keyCode === 120 && evt.altKey) || evt.keyCode === 27) {
|
|
|
|
if ($(':focus').parents('.toolbar').length === 1) {
|
2015-04-03 13:29:47 +02:00
|
|
|
// If we're in the editbar already..
|
|
|
|
// Close any dropdowns we have open..
|
2020-11-23 19:24:19 +01:00
|
|
|
padeditbar.toggleDropDown('none');
|
2015-04-03 13:29:47 +02:00
|
|
|
// Check we're on a pad and not on the timeslider
|
|
|
|
// Or some other window I haven't thought about!
|
2020-11-23 19:24:19 +01:00
|
|
|
if (typeof pad === 'undefined') {
|
2015-04-03 13:29:47 +02:00
|
|
|
// Timeslider probably..
|
|
|
|
// Shift focus away from any drop downs
|
|
|
|
$(':focus').blur(); // required to do not try to remove!
|
2020-04-03 16:11:01 +02:00
|
|
|
$('#editorcontainerbox').focus(); // Focus back onto the pad
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2015-04-03 13:29:47 +02:00
|
|
|
// Shift focus away from any drop downs
|
|
|
|
$(':focus').blur(); // required to do not try to remove!
|
|
|
|
padeditor.ace.focus(); // Sends focus back to pad
|
|
|
|
// The above focus doesn't always work in FF, you have to hit enter afterwards
|
|
|
|
evt.preventDefault();
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2015-04-03 13:29:47 +02:00
|
|
|
// Focus on the editbar :)
|
2020-12-16 13:12:25 +01:00
|
|
|
const firstEditbarElement = parent.parent.$('#editbar button').first();
|
|
|
|
|
2015-04-03 13:29:47 +02:00
|
|
|
$(this).blur();
|
|
|
|
firstEditbarElement.focus();
|
|
|
|
evt.preventDefault();
|
|
|
|
}
|
2015-03-25 12:03:45 +01:00
|
|
|
}
|
2015-04-03 13:29:47 +02:00
|
|
|
// Are we in the toolbar??
|
2020-11-23 19:24:19 +01:00
|
|
|
if ($(':focus').parents('.toolbar').length === 1) {
|
2015-04-03 13:29:47 +02:00
|
|
|
// On arrow keys go to next/previous button item in editbar
|
2020-11-23 19:24:19 +01:00
|
|
|
if (evt.keyCode !== 39 && evt.keyCode !== 37) return;
|
2015-04-03 13:29:47 +02:00
|
|
|
|
|
|
|
// Get all the focusable items in the editbar
|
2020-11-23 19:24:19 +01:00
|
|
|
const focusItems = $('#editbar').find('button, select');
|
2015-04-03 13:29:47 +02:00
|
|
|
|
|
|
|
// On left arrow move to next button in editbar
|
2020-11-23 19:24:19 +01:00
|
|
|
if (evt.keyCode === 37) {
|
2015-04-03 13:29:47 +02:00
|
|
|
// If a dropdown is visible or we're in an input don't move to the next button
|
2020-11-23 19:24:19 +01:00
|
|
|
if ($('.popup').is(':visible') || evt.target.localName === 'input') return;
|
2015-04-03 13:29:47 +02:00
|
|
|
|
|
|
|
editbarPosition--;
|
|
|
|
// Allow focus to shift back to end of row and start of row
|
2020-11-23 19:24:19 +01:00
|
|
|
if (editbarPosition === -1) editbarPosition = focusItems.length - 1;
|
|
|
|
$(focusItems[editbarPosition]).focus();
|
2015-04-03 13:29:47 +02:00
|
|
|
}
|
2015-03-25 12:03:45 +01:00
|
|
|
|
2015-04-03 13:29:47 +02:00
|
|
|
// On right arrow move to next button in editbar
|
2020-11-23 19:24:19 +01:00
|
|
|
if (evt.keyCode === 39) {
|
2015-04-03 13:29:47 +02:00
|
|
|
// If a dropdown is visible or we're in an input don't move to the next button
|
2020-11-23 19:24:19 +01:00
|
|
|
if ($('.popup').is(':visible') || evt.target.localName === 'input') return;
|
2015-03-31 19:50:20 +02:00
|
|
|
|
2015-04-03 13:29:47 +02:00
|
|
|
editbarPosition++;
|
|
|
|
// Allow focus to shift back to end of row and start of row
|
2020-11-23 19:24:19 +01:00
|
|
|
if (editbarPosition >= focusItems.length) editbarPosition = 0;
|
2015-04-03 13:29:47 +02:00
|
|
|
$(focusItems[editbarPosition]).focus();
|
|
|
|
}
|
2015-03-25 12:03:45 +01:00
|
|
|
}
|
2020-12-16 13:12:25 +01:00
|
|
|
};
|
2015-03-25 12:03:45 +01:00
|
|
|
|
2020-12-16 13:12:25 +01:00
|
|
|
const aceAttributeCommand = (cmd, ace) => {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_toggleAttributeOnSelection(cmd);
|
2020-12-16 13:12:25 +01:00
|
|
|
};
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-12-16 13:12:25 +01:00
|
|
|
const registerDefaultCommands = (toolbar) => {
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerDropdownCommand('showusers', 'users');
|
|
|
|
toolbar.registerDropdownCommand('settings');
|
|
|
|
toolbar.registerDropdownCommand('connectivity');
|
|
|
|
toolbar.registerDropdownCommand('import_export');
|
|
|
|
toolbar.registerDropdownCommand('embed');
|
|
|
|
|
|
|
|
toolbar.registerCommand('settings', () => {
|
|
|
|
toolbar.toggleDropDown('settings', () => {
|
2015-03-26 00:30:17 +01:00
|
|
|
$('#options-stickychat').focus();
|
2015-03-31 14:21:41 +02:00
|
|
|
});
|
2015-03-26 00:30:17 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerCommand('import_export', () => {
|
|
|
|
toolbar.toggleDropDown('import_export', () => {
|
2015-03-31 16:00:43 +02:00
|
|
|
// If Import file input exists then focus on it..
|
2020-11-23 19:24:19 +01:00
|
|
|
if ($('#importfileinput').length !== 0) {
|
|
|
|
setTimeout(() => {
|
2015-03-31 16:00:43 +02:00
|
|
|
$('#importfileinput').focus();
|
|
|
|
}, 100);
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2015-03-31 16:00:43 +02:00
|
|
|
$('.exportlink').first().focus();
|
|
|
|
}
|
2015-03-31 14:21:41 +02:00
|
|
|
});
|
2015-03-25 16:38:19 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerCommand('showusers', () => {
|
|
|
|
toolbar.toggleDropDown('users', () => {
|
2015-03-31 14:21:41 +02:00
|
|
|
$('#myusernameedit').focus();
|
|
|
|
});
|
2015-03-25 16:38:19 +01:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerCommand('embed', () => {
|
2013-04-15 20:32:59 +02:00
|
|
|
toolbar.setEmbedLinks();
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.toggleDropDown('embed', () => {
|
2015-03-26 00:30:17 +01:00
|
|
|
$('#linkinput').focus().select();
|
2015-03-31 14:21:41 +02:00
|
|
|
});
|
2013-04-15 20:32:59 +02:00
|
|
|
});
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerCommand('savedRevision', () => {
|
2013-04-15 20:32:59 +02:00
|
|
|
padsavedrevs.saveNow();
|
|
|
|
});
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerCommand('showTimeSlider', () => {
|
|
|
|
document.location = `${document.location.pathname}/timeslider`;
|
2013-04-15 20:32:59 +02:00
|
|
|
});
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('bold', aceAttributeCommand);
|
|
|
|
toolbar.registerAceCommand('italic', aceAttributeCommand);
|
|
|
|
toolbar.registerAceCommand('underline', aceAttributeCommand);
|
|
|
|
toolbar.registerAceCommand('strikethrough', aceAttributeCommand);
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('undo', (cmd, ace) => {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_doUndoRedo(cmd);
|
|
|
|
});
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('redo', (cmd, ace) => {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_doUndoRedo(cmd);
|
|
|
|
});
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('insertunorderedlist', (cmd, ace) => {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_doInsertUnorderedList();
|
|
|
|
});
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('insertorderedlist', (cmd, ace) => {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_doInsertOrderedList();
|
|
|
|
});
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('indent', (cmd, ace) => {
|
2013-04-15 20:32:59 +02:00
|
|
|
if (!ace.ace_doIndentOutdent(false)) {
|
|
|
|
ace.ace_doInsertUnorderedList();
|
2013-03-10 23:42:12 +01:00
|
|
|
}
|
2013-04-15 20:32:59 +02:00
|
|
|
});
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('outdent', (cmd, ace) => {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_doIndentOutdent(true);
|
|
|
|
});
|
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerAceCommand('clearauthorship', (cmd, ace) => {
|
2020-06-05 23:47:12 +02:00
|
|
|
// If we have the whole document selected IE control A has been hit
|
2020-11-23 19:24:19 +01:00
|
|
|
const rep = ace.ace_getRep();
|
2020-12-16 13:12:25 +01:00
|
|
|
let doPrompt = false;
|
2020-11-23 19:24:19 +01:00
|
|
|
const lastChar = rep.lines.atIndex(rep.lines.length() - 1).width - 1;
|
|
|
|
const lastLineIndex = rep.lines.length() - 1;
|
|
|
|
if (rep.selStart[0] === 0 && rep.selStart[1] === 0) {
|
2020-06-05 23:47:12 +02:00
|
|
|
// nesting intentionally here to make things readable
|
2020-11-23 19:24:19 +01:00
|
|
|
if (rep.selEnd[0] === lastLineIndex && rep.selEnd[1] === lastChar) {
|
2020-12-16 13:12:25 +01:00
|
|
|
doPrompt = true;
|
2020-06-05 23:47:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* NOTICE: This command isn't fired on Control Shift C.
|
|
|
|
* I intentionally didn't create duplicate code because if you are hitting
|
|
|
|
* Control Shift C we make the assumption you are a "power user"
|
|
|
|
* and as such we assume you don't need the prompt to bug you each time!
|
|
|
|
* This does make wonder if it's worth having a checkbox to avoid being
|
|
|
|
* prompted again but that's probably overkill for this contribution.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// if we don't have any text selected, we have a caret or we have already said to prompt
|
|
|
|
if ((!(rep.selStart && rep.selEnd)) || ace.ace_isCaret() || doPrompt) {
|
2020-11-23 19:24:19 +01:00
|
|
|
if (window.confirm(html10n.get('pad.editbar.clearcolors'))) {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_performDocumentApplyAttributesToCharRange(0, ace.ace_getRep().alltext.length, [
|
2020-11-23 19:24:19 +01:00
|
|
|
['author', ''],
|
2013-04-15 20:32:59 +02:00
|
|
|
]);
|
|
|
|
}
|
2020-11-23 19:24:19 +01:00
|
|
|
} else {
|
2013-04-15 20:32:59 +02:00
|
|
|
ace.ace_setAttributeOnSelection('author', '');
|
|
|
|
}
|
|
|
|
});
|
2014-03-30 13:02:41 +02:00
|
|
|
|
2020-11-23 19:24:19 +01:00
|
|
|
toolbar.registerCommand('timeslider_returnToPad', (cmd) => {
|
2020-12-16 13:12:25 +01:00
|
|
|
if (document.referrer.length > 0 &&
|
|
|
|
document.referrer.substring(document.referrer.lastIndexOf('/') - 1,
|
|
|
|
document.referrer.lastIndexOf('/')) === 'p') {
|
2014-03-30 13:02:41 +02:00
|
|
|
document.location = document.referrer;
|
|
|
|
} else {
|
2020-12-16 13:12:25 +01:00
|
|
|
document.location = document.location.href
|
|
|
|
.substring(0, document.location.href.lastIndexOf('/'));
|
2014-03-30 13:02:41 +02:00
|
|
|
}
|
|
|
|
});
|
2020-12-16 13:12:25 +01:00
|
|
|
};
|
2013-03-10 23:42:12 +01:00
|
|
|
|
2011-03-26 14:10:41 +01:00
|
|
|
return self;
|
2011-03-26 15:50:13 +01:00
|
|
|
}());
|
2012-01-16 02:23:48 +01:00
|
|
|
|
|
|
|
exports.padeditbar = padeditbar;
|