Add: source editor

This commit is contained in:
eight 2017-09-12 00:09:25 +08:00
parent 8bc6986cac
commit a15493bfb9
6 changed files with 287 additions and 15 deletions

View File

@ -11,6 +11,8 @@
<script src="content/apply.js"></script> <script src="content/apply.js"></script>
<link rel="stylesheet" href="edit/edit.css"> <link rel="stylesheet" href="edit/edit.css">
<script src="edit/lint.js"></script> <script src="edit/lint.js"></script>
<script src="edit/util.js"></script>
<script src="edit/source-editor.js"></script>
<script src="edit/edit.js"></script> <script src="edit/edit.js"></script>
<script src="vendor/codemirror/lib/codemirror.js"></script> <script src="vendor/codemirror/lib/codemirror.js"></script>
@ -41,6 +43,8 @@
<script src="vendor/codemirror/addon/hint/show-hint.js"></script> <script src="vendor/codemirror/addon/hint/show-hint.js"></script>
<script src="vendor/codemirror/addon/hint/css-hint.js"></script> <script src="vendor/codemirror/addon/hint/css-hint.js"></script>
<script src="vendor/codemirror/addon/mode/loadmode.js"></script>
<script src="vendor/codemirror/keymap/sublime.js"></script> <script src="vendor/codemirror/keymap/sublime.js"></script>
<script src="vendor/codemirror/keymap/emacs.js"></script> <script src="vendor/codemirror/keymap/emacs.js"></script>
<script src="vendor/codemirror/keymap/vim.js"></script> <script src="vendor/codemirror/keymap/vim.js"></script>

View File

@ -496,6 +496,17 @@ body[data-match-highlight="selection"] .CodeMirror-selection-highlight-scrollbar
background-color: rgba(0, 0, 0, 0.05); background-color: rgba(0, 0, 0, 0.05);
} }
/************ single editor **************/
#sections .single-editor {
margin: 0;
height: 100%;
box-sizing: border-box;
}
.single-editor .CodeMirror {
height: 100%;
}
/************ reponsive layouts ************/ /************ reponsive layouts ************/
@media(max-width:737px) { @media(max-width:737px) {
#header { #header {

View File

@ -3,7 +3,7 @@
/* global onDOMscripted */ /* global onDOMscripted */
/* global css_beautify */ /* global css_beautify */
/* global CSSLint initLint linterConfig updateLintReport renderLintReport updateLinter */ /* global CSSLint initLint linterConfig updateLintReport renderLintReport updateLinter */
/* global mozParser */ /* global mozParser createSourceEditor */
'use strict'; 'use strict';
@ -20,6 +20,8 @@ let useHistoryBack;
const propertyToCss = {urls: 'url', urlPrefixes: 'url-prefix', domains: 'domain', regexps: 'regexp'}; const propertyToCss = {urls: 'url', urlPrefixes: 'url-prefix', domains: 'domain', regexps: 'regexp'};
const CssToProperty = {'url': 'urls', 'url-prefix': 'urlPrefixes', 'domain': 'domains', 'regexp': 'regexps'}; const CssToProperty = {'url': 'urls', 'url-prefix': 'urlPrefixes', 'domain': 'domains', 'regexp': 'regexps'};
let editor;
// if background page hasn't been loaded yet, increase the chances it has before DOMContentLoaded // if background page hasn't been loaded yet, increase the chances it has before DOMContentLoaded
onBackgroundReady(); onBackgroundReady();
@ -271,11 +273,13 @@ function initCodeMirror() {
CM.getOption = o => CodeMirror.defaults[o]; CM.getOption = o => CodeMirror.defaults[o];
CM.setOption = (o, v) => { CM.setOption = (o, v) => {
CodeMirror.defaults[o] = v; CodeMirror.defaults[o] = v;
editors.forEach(editor => { $$('.CodeMirror').map(e => e.CodeMirror).forEach(editor => {
editor.setOption(o, v); editor.setOption(o, v);
}); });
}; };
CM.modeURL = '/vendor/codemirror/mode/%N/%N.js';
CM.prototype.getSection = function () { CM.prototype.getSection = function () {
return this.display.wrapper.parentNode; return this.display.wrapper.parentNode;
}; };
@ -355,11 +359,9 @@ function acmeEventListener(event) {
return; return;
} }
case 'autocompleteOnTyping': case 'autocompleteOnTyping':
editors.forEach(cm => { $$('.CodeMirror')
const onOff = el.checked ? 'on' : 'off'; .map(e => e.CodeMirror)
cm[onOff]('change', autocompleteOnTyping); .forEach(cm => setupAutocomplete(cm, el.checked));
cm[onOff]('pick', autocompletePicked);
});
return; return;
case 'matchHighlight': case 'matchHighlight':
switch (value) { switch (value) {
@ -384,8 +386,7 @@ function setupCodeMirror(textarea, index) {
cm.on('change', indicateCodeChange); cm.on('change', indicateCodeChange);
if (prefs.get('editor.autocompleteOnTyping')) { if (prefs.get('editor.autocompleteOnTyping')) {
cm.on('change', autocompleteOnTyping); setupAutocomplete(cm);
cm.on('pick', autocompletePicked);
} }
cm.on('blur', () => { cm.on('blur', () => {
editors.lastActive = cm; editors.lastActive = cm;
@ -996,6 +997,13 @@ function jumpToLine(cm) {
} }
function toggleStyle() { function toggleStyle() {
if (!editor) {
return _toggleStyle();
}
editor.toggleStyle();
}
function _toggleStyle() {
$('#enabled').checked = !$('#enabled').checked; $('#enabled').checked = !$('#enabled').checked;
save(); save();
} }
@ -1021,6 +1029,12 @@ function toggleSectionHeight(cm) {
} }
} }
function setupAutocomplete(cm, enable = true) {
const onOff = enable ? 'on' : 'off';
cm[onOff]('change', autocompleteOnTyping);
cm[onOff]('pick', autocompletePicked);
}
function autocompleteOnTyping(cm, info, debounced) { function autocompleteOnTyping(cm, info, debounced) {
if ( if (
cm.state.completionActive || cm.state.completionActive ||
@ -1079,7 +1093,7 @@ function getEditorInSight(nearbyElement) {
cm = editors.lastActive; cm = editors.lastActive;
} }
if (!cm || offscreenDistance(cm) > 0) { if (!cm || offscreenDistance(cm) > 0) {
const sorted = editors const sorted = $$('#sections .CodeMirror').map(e => e.CodeMirror)
.map((cm, index) => ({cm: cm, distance: offscreenDistance(cm), index: index})) .map((cm, index) => ({cm: cm, distance: offscreenDistance(cm), index: index}))
.sort((a, b) => a.distance - b.distance || a.index - b.index); .sort((a, b) => a.distance - b.distance || a.index - b.index);
cm = sorted[0].cm; cm = sorted[0].cm;
@ -1120,7 +1134,7 @@ function beautify(event) {
options.indent_char = tabs ? '\t' : ' '; options.indent_char = tabs ? '\t' : ' ';
const section = getSectionForChild(event.target); const section = getSectionForChild(event.target);
const scope = section ? [section.CodeMirror] : editors; const scope = section ? [section.CodeMirror] : $$('#sections .CodeMirror').map(e => e.CodeMirror);
showHelp(t('styleBeautify'), '<div class="beautify-options">' + showHelp(t('styleBeautify'), '<div class="beautify-options">' +
optionHtml('.selector1,', 'selector_separator_newline') + optionHtml('.selector1,', 'selector_separator_newline') +
@ -1261,7 +1275,20 @@ function setStyleMeta(style) {
$('#url').href = style.url; $('#url').href = style.url;
} }
function initWithStyle({style, codeIsUpdated}) { function initWithStyle({style}) {
// FIXME: what does codeIsUpdated do?
if (!style.usercss) {
return _initWithStyle({style});
}
if (editor) {
editor.replaceStyle(style);
} else {
editor = createSourceEditor(style);
}
}
function _initWithStyle({style, codeIsUpdated}) {
setStyleMeta(style); setStyleMeta(style);
if (codeIsUpdated === false) { if (codeIsUpdated === false) {
@ -1440,6 +1467,13 @@ function updateLintReportIfEnabled(cm, time) {
} }
function save() { function save() {
if (!editor) {
return _save();
}
editor.save();
}
function _save() {
updateLintReportIfEnabled(null, 0); updateLintReportIfEnabled(null, 0);
// save the contents of the CodeMirror editors back into the textareas // save the contents of the CodeMirror editors back into the textareas

View File

@ -147,7 +147,7 @@ function updateLinter({immediately} = {}) {
function updateEditors() { function updateEditors() {
CodeMirror.defaults.lint = linterConfig.getForCodeMirror(linter); CodeMirror.defaults.lint = linterConfig.getForCodeMirror(linter);
const guttersOption = prepareGuttersOption(); const guttersOption = prepareGuttersOption();
editors.forEach(cm => { $$('#sections .CodeMirror').map(e => e.CodeMirror).forEach(cm => {
cm.setOption('lint', CodeMirror.defaults.lint); cm.setOption('lint', CodeMirror.defaults.lint);
if (guttersOption) { if (guttersOption) {
cm.setOption('guttersOption', guttersOption); cm.setOption('guttersOption', guttersOption);
@ -217,7 +217,7 @@ function updateLintReport(cm, delay) {
state.postponeNewIssues = delay === undefined || delay === null; state.postponeNewIssues = delay === undefined || delay === null;
function update(cm) { function update(cm) {
const scope = cm ? [cm] : editors; const scope = cm ? [cm] : $$('#sections .CodeMirror').map(e => e.CodeMirror);
let changed = false; let changed = false;
let fixedOldIssues = false; let fixedOldIssues = false;
scope.forEach(cm => { scope.forEach(cm => {
@ -284,7 +284,7 @@ function renderLintReport(someBlockChanged) {
const label = t('sectionCode'); const label = t('sectionCode');
const newContent = content.cloneNode(false); const newContent = content.cloneNode(false);
let issueCount = 0; let issueCount = 0;
editors.forEach((cm, index) => { $$('#sections .CodeMirror').map(e => e.CodeMirror).forEach((cm, index) => {
if (cm.state.lint && cm.state.lint.html) { if (cm.state.lint && cm.state.lint.html) {
const html = '<caption>' + label + ' ' + (index + 1) + '</caption>' + cm.state.lint.html; const html = '<caption>' + label + ' ' + (index + 1) + '</caption>' + cm.state.lint.html;
const newBlock = newContent.appendChild(tHTML(html, 'table')); const newBlock = newContent.appendChild(tHTML(html, 'table'));

133
edit/source-editor.js Normal file
View File

@ -0,0 +1,133 @@
/* global CodeMirror dirtyReporter initLint beautify showKeyMapHelp */
/* global showToggleStyleHelp goBackToManage updateLintReportIfEnabled */
/* global hotkeyRerouter setupAutocomplete */
'use strict';
function createSourceEditor(style) {
// draw HTML
$('#sections').innerHTML = '';
$('#name').disabled = true;
$('#mozilla-format-heading').parentNode.remove();
$('#sections').appendChild(tHTML(`
<div class="single-editor">
<textarea></textarea>
</div>
`));
// draw CodeMirror
$('#sections textarea').value = style.source;
const cm = CodeMirror.fromTextArea($('#sections textarea'));
// dirty reporter
const dirty = dirtyReporter();
dirty.onChange(() => {
const DIRTY = dirty.isDirty();
document.title = (DIRTY ? '* ' : '') + t('editStyleTitle', [style.name]);
document.body.classList.toggle('dirty', DIRTY);
$('#save-button').disabled = !DIRTY;
});
// draw metas info
updateMetas();
initHooks();
initLint();
function initHooks() {
// sidebar commands
$('#save-button').onclick = save;
$('#beautify').onclick = beautify;
$('#keyMap-help').onclick = showKeyMapHelp;
$('#toggle-style-help').onclick = showToggleStyleHelp;
$('#cancel-button').onclick = goBackToManage;
// enable
$('#enabled').onchange = e => {
const value = e.target.checked;
dirty.modify('enabled', style.enabled, value);
style.enabled = value;
};
// source
cm.on('change', () => {
const value = cm.getValue();
dirty.modify('source', style.source, value);
style.source = value;
updateLintReportIfEnabled(cm);
});
// hotkeyRerouter
cm.on('focus', () => {
hotkeyRerouter.setState(false);
});
cm.on('blur', () => {
hotkeyRerouter.setState(true);
});
// autocomplete
if (prefs.get('editor.autocompleteOnTyping')) {
setupAutocomplete(cm);
}
}
function updateMetas() {
$('#name').value = style.name;
$('#enabled').checked = style.enabled;
$('#url').href = style.url;
cm.setOption('mode', style.preprocessor || 'css');
CodeMirror.autoLoadMode(cm, style.preprocessor || 'css');
// beautify only works with regular CSS
$('#beautify').disabled = Boolean(style.preprocessor);
}
function replaceStyle(_style) {
style = _style;
updateMetas();
if (style.source !== cm.getValue()) {
const cursor = cm.getCursor();
cm.setValue(style.source);
cm.setCursor(cursor);
}
dirty.clear();
}
function toggleStyle() {
const value = !style.enabled;
dirty.modify('enabled', style.enabled, value);
style.enabled = value;
updateMetas();
// save when toggle enable state?
save();
}
function save() {
if (!dirty.isDirty()) {
return;
}
const req = {
method: 'saveUsercss',
reason: 'editSave',
id: style.id,
enabled: style.enabled,
source: style.source
};
return onBackgroundReady().then(() => BG.saveUsercss(req))
.then(result => {
if (result.status === 'error') {
throw new Error(result.error);
}
return result;
})
.then(({style}) => {
replaceStyle(style);
})
.catch(err => {
console.error(err);
alert(err);
});
}
return {replaceStyle, save, toggleStyle};
}

90
edit/util.js Normal file
View File

@ -0,0 +1,90 @@
'use strict';
function dirtyReporter() {
const dirty = new Map();
const onchanges = [];
function add(obj, value) {
const saved = dirty.get(obj);
if (!saved) {
dirty.set(obj, {type: 'add', newValue: value});
} else if (saved.type === 'remove') {
if (saved.savedValue === value) {
dirty.delete(obj);
} else {
saved.newValue = value;
saved.type = 'modify';
}
}
}
function remove(obj, value) {
const saved = dirty.get(obj);
if (!saved) {
dirty.set(obj, {type: 'remove', savedValue: value});
} else if (saved.type === 'add') {
dirty.delete(obj);
} else if (saved.type === 'modify') {
saved.type = 'remove';
}
}
function modify(obj, oldValue, newValue) {
const saved = dirty.get(obj);
if (!saved) {
if (oldValue !== newValue) {
dirty.set(obj, {type: 'modify', savedValue: oldValue, newValue});
}
} else if (saved.type === 'modify') {
if (saved.savedValue === newValue) {
dirty.delete(obj);
} else {
saved.newValue = newValue;
}
} else if (saved.type === 'add') {
saved.newValue = newValue;
}
}
function clear() {
dirty.clear();
}
function isDirty() {
return dirty.size > 0;
}
function onChange(cb) {
onchanges.push(cb);
}
function wrap(obj) {
for (const key of ['add', 'remove', 'modify', 'clear']) {
obj[key] = trackChange(obj[key]);
}
return obj;
}
function emitChange() {
for (const cb of onchanges) {
try {
cb();
} catch (err) {
console.error(err);
}
}
}
function trackChange(fn) {
return function () {
const dirty = isDirty();
const result = fn.apply(null, arguments);
if (dirty !== isDirty()) {
emitChange();
}
return result;
};
}
return wrap({add, remove, modify, clear, isDirty, onChange});
}