2017-08-26 14:49:14 +00:00
|
|
|
/* global CodeMirror messageBox */
|
|
|
|
/* global editors makeSectionVisible showCodeMirrorPopup showHelp */
|
2017-09-12 15:19:16 +00:00
|
|
|
/* global loadScript require CSSLint stylelint */
|
2017-09-13 09:34:27 +00:00
|
|
|
/* global makeLink */
|
2017-08-16 21:01:45 +00:00
|
|
|
'use strict';
|
|
|
|
|
2017-09-13 08:53:21 +00:00
|
|
|
onDOMready().then(loadLinterAssets);
|
2017-08-29 14:12:39 +00:00
|
|
|
|
2017-08-28 05:22:19 +00:00
|
|
|
// eslint-disable-next-line no-var
|
|
|
|
var linterConfig = {
|
|
|
|
csslint: {},
|
|
|
|
stylelint: {},
|
|
|
|
defaults: {
|
|
|
|
// set in lint-defaults-csslint.js
|
|
|
|
csslint: {},
|
|
|
|
// set in lint-defaults-stylelint.js
|
|
|
|
stylelint: {},
|
|
|
|
},
|
|
|
|
storageName: {
|
|
|
|
csslint: 'editorCSSLintConfig',
|
|
|
|
stylelint: 'editorStylelintConfig',
|
|
|
|
},
|
|
|
|
|
|
|
|
getCurrent(linter = prefs.get('editor.linter')) {
|
|
|
|
return this.fallbackToDefaults(this[linter] || {});
|
|
|
|
},
|
|
|
|
|
|
|
|
getForCodeMirror(linter = prefs.get('editor.linter')) {
|
|
|
|
return CodeMirror.lint && CodeMirror.lint[linter] ? {
|
|
|
|
getAnnotations: CodeMirror.lint[linter],
|
|
|
|
delay: prefs.get('editor.lintDelay'),
|
|
|
|
} : false;
|
|
|
|
},
|
|
|
|
|
|
|
|
fallbackToDefaults(config, linter = prefs.get('editor.linter')) {
|
|
|
|
if (config && Object.keys(config).length) {
|
|
|
|
if (linter === 'stylelint') {
|
|
|
|
// always use default syntax because we don't expose it in config UI
|
|
|
|
config.syntax = this.defaults.stylelint.syntax;
|
|
|
|
}
|
|
|
|
return config;
|
|
|
|
} else {
|
|
|
|
return deepCopy(this.defaults[linter] || {});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
setLinter(linter = prefs.get('editor.linter')) {
|
|
|
|
linter = linter.toLowerCase();
|
|
|
|
linter = linter === 'csslint' || linter === 'stylelint' ? linter : '';
|
|
|
|
if (prefs.get('editor.linter') !== linter) {
|
|
|
|
prefs.set('editor.linter', linter);
|
|
|
|
}
|
|
|
|
return linter;
|
|
|
|
},
|
|
|
|
|
|
|
|
findInvalidRules(config, linter = prefs.get('editor.linter')) {
|
|
|
|
const rules = linter === 'stylelint' ? config.rules : config;
|
|
|
|
const allRules = new Set(
|
|
|
|
linter === 'stylelint'
|
|
|
|
? Object.keys(stylelint.rules)
|
|
|
|
: CSSLint.getRules().map(rule => rule.id)
|
|
|
|
);
|
|
|
|
return Object.keys(rules).filter(rule => !allRules.has(rule));
|
|
|
|
},
|
|
|
|
|
|
|
|
stringify(config = this.getCurrent()) {
|
|
|
|
if (prefs.get('editor.linter') === 'stylelint') {
|
|
|
|
config.syntax = undefined;
|
|
|
|
}
|
|
|
|
return JSON.stringify(config, null, 2)
|
|
|
|
.replace(/,\n\s+\{\n\s+("severity":\s"\w+")\n\s+\}/g, ', {$1}');
|
|
|
|
},
|
|
|
|
|
|
|
|
save(config) {
|
|
|
|
config = this.fallbackToDefaults(config);
|
|
|
|
const linter = prefs.get('editor.linter');
|
|
|
|
this[linter] = config;
|
|
|
|
BG.chromeSync.setLZValue(this.storageName[linter], config);
|
|
|
|
return config;
|
|
|
|
},
|
|
|
|
|
|
|
|
loadAll() {
|
|
|
|
return BG.chromeSync.getLZValues([
|
|
|
|
'editorCSSLintConfig',
|
|
|
|
'editorStylelintConfig',
|
|
|
|
]).then(data => {
|
|
|
|
this.csslint = this.fallbackToDefaults(data.editorCSSLintConfig, 'csslint');
|
|
|
|
this.stylelint = this.fallbackToDefaults(data.editorStylelintConfig, 'stylelint');
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
watchStorage() {
|
|
|
|
chrome.storage.onChanged.addListener((changes, area) => {
|
|
|
|
if (area === 'sync') {
|
|
|
|
for (const name of ['editorCSSLintConfig', 'editorStylelintConfig']) {
|
|
|
|
if (name in changes && changes[name].newValue !== changes[name].oldValue) {
|
2017-08-29 14:59:45 +00:00
|
|
|
this.loadAll().then(updateLinter);
|
2017-08-28 05:22:19 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
// this is an event listener so it can't refer to self via 'this'
|
|
|
|
openOnClick() {
|
|
|
|
setupLinterPopup(linterConfig.stringify());
|
|
|
|
},
|
|
|
|
|
|
|
|
showSavedMessage() {
|
|
|
|
$('#help-popup .saved-message').classList.add('show');
|
|
|
|
clearTimeout($('#help-popup .contents').timer);
|
|
|
|
$('#help-popup .contents').timer = setTimeout(() => {
|
|
|
|
// popup may be closed at this point
|
|
|
|
const msg = $('#help-popup .saved-message');
|
|
|
|
if (msg) {
|
|
|
|
msg.classList.remove('show');
|
|
|
|
}
|
|
|
|
}, 2000);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2017-08-17 19:08:48 +00:00
|
|
|
function initLint() {
|
2017-08-20 18:32:41 +00:00
|
|
|
$('#lint-help').addEventListener('click', showLintHelp);
|
|
|
|
$('#lint').addEventListener('click', gotoLintIssue);
|
2017-08-28 05:22:19 +00:00
|
|
|
$('#linter-settings').addEventListener('click', linterConfig.openOnClick);
|
2017-08-16 21:01:45 +00:00
|
|
|
window.addEventListener('resize', resizeLintReport);
|
|
|
|
|
|
|
|
// touch devices don't have onHover events so the element we'll be toggled via clicking (touching)
|
|
|
|
if ('ontouchstart' in document.body) {
|
2017-08-20 18:32:41 +00:00
|
|
|
$('#lint h2').addEventListener('click', toggleLintReport);
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-17 19:08:48 +00:00
|
|
|
|
2017-08-28 05:22:19 +00:00
|
|
|
linterConfig.loadAll();
|
|
|
|
linterConfig.watchStorage();
|
2017-09-03 17:06:20 +00:00
|
|
|
prefs.subscribe(['editor.linter'], updateLinter);
|
2017-08-29 14:59:45 +00:00
|
|
|
updateLinter();
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
2017-08-29 14:59:45 +00:00
|
|
|
function updateLinter({immediately} = {}) {
|
|
|
|
if (!immediately) {
|
|
|
|
debounce(updateLinter, 0, {immediately: true});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const linter = prefs.get('editor.linter');
|
2017-08-29 14:12:39 +00:00
|
|
|
const GUTTERS_CLASS = 'CodeMirror-lint-markers';
|
|
|
|
|
2017-09-03 22:07:07 +00:00
|
|
|
loadLinterAssets(linter).then(updateEditors);
|
|
|
|
$('#linter-settings').style.display = !linter ? 'none' : 'inline-block';
|
|
|
|
$('#lint').style.display = 'none';
|
|
|
|
|
2017-08-20 14:06:17 +00:00
|
|
|
function updateEditors() {
|
2017-08-29 14:12:39 +00:00
|
|
|
CodeMirror.defaults.lint = linterConfig.getForCodeMirror(linter);
|
|
|
|
const guttersOption = prepareGuttersOption();
|
2017-09-16 00:44:56 +00:00
|
|
|
editors.forEach(cm => {
|
2017-08-29 14:12:39 +00:00
|
|
|
cm.setOption('lint', CodeMirror.defaults.lint);
|
|
|
|
if (guttersOption) {
|
|
|
|
cm.setOption('guttersOption', guttersOption);
|
|
|
|
updateGutters(cm, guttersOption);
|
2017-08-31 19:25:05 +00:00
|
|
|
cm.refresh();
|
2017-08-29 14:12:39 +00:00
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
setTimeout(updateLintReport, 0, cm);
|
2017-08-20 14:06:17 +00:00
|
|
|
});
|
|
|
|
}
|
2017-08-29 14:12:39 +00:00
|
|
|
|
|
|
|
function prepareGuttersOption() {
|
|
|
|
const gutters = CodeMirror.defaults.gutters;
|
|
|
|
const needRefresh = Boolean(linter) !== gutters.includes(GUTTERS_CLASS);
|
|
|
|
if (needRefresh) {
|
|
|
|
if (linter) {
|
|
|
|
gutters.push(GUTTERS_CLASS);
|
|
|
|
} else {
|
|
|
|
gutters.splice(gutters.indexOf(GUTTERS_CLASS), 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return needRefresh && gutters;
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateGutters(cm, guttersOption) {
|
|
|
|
cm.options.gutters = guttersOption;
|
|
|
|
const el = $('.' + GUTTERS_CLASS, cm.display.gutters);
|
|
|
|
if (linter && !el) {
|
|
|
|
cm.display.gutters.appendChild($element({
|
|
|
|
className: 'CodeMirror-gutter ' + GUTTERS_CLASS
|
|
|
|
}));
|
|
|
|
} else if (!linter && el) {
|
|
|
|
el.remove();
|
|
|
|
}
|
|
|
|
}
|
2017-08-17 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 21:01:45 +00:00
|
|
|
function updateLintReport(cm, delay) {
|
2017-08-29 20:36:56 +00:00
|
|
|
if (cm && !cm.options.lint) {
|
|
|
|
// add 'lint' option back to the freshly created section
|
|
|
|
setTimeout(() => {
|
|
|
|
if (!cm.options.lint) {
|
|
|
|
cm.setOption('lint', linterConfig.getForCodeMirror());
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const state = cm && cm.state && cm.state.lint || {};
|
2017-08-16 21:01:45 +00:00
|
|
|
if (delay === 0) {
|
|
|
|
// immediately show pending csslint/stylelint messages in onbeforeunload and save
|
2017-08-29 20:36:56 +00:00
|
|
|
clearTimeout(state.lintTimeout);
|
2017-08-31 19:25:05 +00:00
|
|
|
updateLintReportInternal(cm);
|
2017-08-16 21:01:45 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (delay > 0) {
|
2017-08-29 20:36:56 +00:00
|
|
|
clearTimeout(state.lintTimeout);
|
|
|
|
state.lintTimeout = setTimeout(cm => {
|
2017-08-29 14:12:39 +00:00
|
|
|
if (cm.performLint) {
|
|
|
|
cm.performLint();
|
2017-08-31 19:25:05 +00:00
|
|
|
updateLintReportInternal(cm);
|
2017-08-29 14:12:39 +00:00
|
|
|
}
|
2017-08-20 19:10:58 +00:00
|
|
|
}, delay, cm);
|
2017-08-16 21:01:45 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
if (state.options) {
|
|
|
|
clearTimeout(state.reportTimeout);
|
|
|
|
const delay = cm && cm.state.renderLintReportNow ? 0 : state.options.delay + 100;
|
|
|
|
state.reportTimeout = setTimeout(updateLintReportInternal, delay, cm, {
|
|
|
|
postponeNewIssues: delay === undefined || delay === null
|
|
|
|
});
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
}
|
2017-08-16 21:01:45 +00:00
|
|
|
|
2017-08-31 19:25:05 +00:00
|
|
|
function updateLintReportInternal(scope, {postponeNewIssues} = {}) {
|
2017-09-03 16:36:33 +00:00
|
|
|
const {changed, fixedSome} = (scope ? [scope] : editors).reduce(process, {});
|
|
|
|
if (changed) {
|
|
|
|
const renderNow = editors.last.state.renderLintReportNow =
|
|
|
|
!postponeNewIssues || fixedSome || editors.last.state.renderLintReportNow;
|
|
|
|
debounce(renderLintReport, renderNow ? 0 : CodeMirror.defaults.lintReportDelay, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
function process(result, cm) {
|
2017-08-31 19:25:05 +00:00
|
|
|
const lintState = cm.state.lint || {};
|
2017-09-03 16:36:33 +00:00
|
|
|
const oldMarkers = lintState.stylusMarkers || new Map();
|
|
|
|
const newMarkers = lintState.stylusMarkers = new Map();
|
|
|
|
const oldText = (lintState.body || {}).textContentCached || '';
|
2017-08-31 19:25:05 +00:00
|
|
|
const activeLine = cm.getCursor().line;
|
2017-09-03 16:36:33 +00:00
|
|
|
const body = !(lintState.marked || {}).length ? {} : $element({
|
|
|
|
tag: 'tbody',
|
|
|
|
appendChild: lintState.marked.map(mark => {
|
|
|
|
const info = mark.__annotation;
|
|
|
|
const {line, ch} = info.from;
|
|
|
|
const isActiveLine = line === activeLine;
|
|
|
|
const pos = isActiveLine ? 'cursor' : (line + ',' + ch);
|
|
|
|
const title = clipString(info.message, 1000) + `\n(${info.rule})`;
|
|
|
|
const message = clipString(info.message, 100);
|
|
|
|
if (isActiveLine || oldMarkers[pos] === message) {
|
|
|
|
oldMarkers.delete(pos);
|
|
|
|
}
|
|
|
|
newMarkers.set(pos, message);
|
|
|
|
return $element({
|
|
|
|
tag: 'tr',
|
|
|
|
className: info.severity,
|
|
|
|
appendChild: [
|
|
|
|
$element({
|
|
|
|
tag: 'td',
|
|
|
|
attributes: {role: 'severity'},
|
|
|
|
dataset: {rule: info.rule},
|
|
|
|
appendChild: $element({
|
|
|
|
className: 'CodeMirror-lint-marker-' + info.severity,
|
|
|
|
textContent: info.severity,
|
2017-08-31 19:25:05 +00:00
|
|
|
}),
|
2017-09-03 16:36:33 +00:00
|
|
|
}),
|
|
|
|
$element({tag: 'td', attributes: {role: 'line'}, textContent: line + 1}),
|
|
|
|
$element({tag: 'td', attributes: {role: 'sep'}, textContent: ':'}),
|
|
|
|
$element({tag: 'td', attributes: {role: 'col'}, textContent: ch + 1}),
|
|
|
|
$element({tag: 'td', attributes: {role: 'message'}, textContent: message, title}),
|
|
|
|
],
|
|
|
|
});
|
|
|
|
})
|
2017-08-16 21:01:45 +00:00
|
|
|
});
|
2017-09-03 16:36:33 +00:00
|
|
|
body.textContentCached = body.textContent || '';
|
|
|
|
lintState.body = body.textContentCached && body;
|
|
|
|
result.changed |= oldText !== body.textContentCached;
|
|
|
|
result.fixedSome |= lintState.reportDisplayed && oldMarkers.size;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
function clipString(str, limit) {
|
|
|
|
return str.length <= limit ? str : str.substr(0, limit) + '...';
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function renderLintReport(someBlockChanged) {
|
2017-08-20 18:32:41 +00:00
|
|
|
const container = $('#lint');
|
2017-08-16 21:01:45 +00:00
|
|
|
const content = container.children[1];
|
|
|
|
const label = t('sectionCode');
|
|
|
|
const newContent = content.cloneNode(false);
|
|
|
|
let issueCount = 0;
|
|
|
|
editors.forEach((cm, index) => {
|
2017-09-03 16:36:33 +00:00
|
|
|
cm.state.renderLintReportNow = false;
|
2017-08-31 19:25:05 +00:00
|
|
|
const lintState = cm.state.lint || {};
|
|
|
|
const body = lintState.body;
|
|
|
|
if (!body) {
|
|
|
|
return;
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-31 19:25:05 +00:00
|
|
|
const newBlock = $element({
|
|
|
|
tag: 'table',
|
|
|
|
appendChild: [
|
|
|
|
$element({tag: 'caption', textContent: label + ' ' + (index + 1)}),
|
|
|
|
body,
|
|
|
|
],
|
|
|
|
cm,
|
|
|
|
});
|
|
|
|
newContent.appendChild(newBlock);
|
|
|
|
issueCount += newBlock.rows.length;
|
|
|
|
|
|
|
|
const block = content.children[newContent.children.length - 1];
|
|
|
|
const blockChanged =
|
|
|
|
!block ||
|
|
|
|
block.cm !== cm ||
|
|
|
|
body.textContentCached !== block.textContentCached;
|
|
|
|
someBlockChanged |= blockChanged;
|
|
|
|
lintState.reportDisplayed = blockChanged;
|
2017-08-16 21:01:45 +00:00
|
|
|
});
|
|
|
|
if (someBlockChanged || newContent.children.length !== content.children.length) {
|
2017-08-20 18:32:41 +00:00
|
|
|
$('#issue-count').textContent = issueCount;
|
2017-08-16 21:01:45 +00:00
|
|
|
container.replaceChild(newContent, content);
|
|
|
|
container.style.display = newContent.children.length ? 'block' : 'none';
|
2017-08-17 19:47:45 +00:00
|
|
|
resizeLintReport();
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-17 19:47:45 +00:00
|
|
|
function resizeLintReport() {
|
2017-08-20 18:56:18 +00:00
|
|
|
// subtracted value to prevent scrollbar
|
|
|
|
const magicBuffer = 20;
|
2017-08-17 19:47:45 +00:00
|
|
|
const content = $('#lint table');
|
2017-08-18 14:35:02 +00:00
|
|
|
if (content) {
|
2017-08-16 21:01:45 +00:00
|
|
|
const bounds = content.getBoundingClientRect();
|
2017-08-17 19:47:45 +00:00
|
|
|
const newMaxHeight = bounds.bottom <= window.innerHeight ? '' :
|
|
|
|
// subtract out a bit of padding or the vertical scrollbar extends beyond the viewport
|
|
|
|
(window.innerHeight - bounds.top - magicBuffer) + 'px';
|
2017-08-16 21:01:45 +00:00
|
|
|
if (newMaxHeight !== content.style.maxHeight) {
|
2017-08-17 19:47:45 +00:00
|
|
|
content.parentNode.style.maxHeight = newMaxHeight;
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function gotoLintIssue(event) {
|
|
|
|
const issue = event.target.closest('tr');
|
|
|
|
if (!issue) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const block = issue.closest('table');
|
|
|
|
makeSectionVisible(block.cm);
|
|
|
|
block.cm.focus();
|
|
|
|
block.cm.setSelection({
|
2017-08-20 18:32:41 +00:00
|
|
|
line: parseInt($('td[role="line"]', issue).textContent) - 1,
|
|
|
|
ch: parseInt($('td[role="col"]', issue).textContent) - 1
|
2017-08-16 21:01:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function toggleLintReport() {
|
2017-08-20 18:32:41 +00:00
|
|
|
$('#lint').classList.toggle('collapsed');
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function showLintHelp() {
|
2017-08-23 22:13:55 +00:00
|
|
|
const linter = prefs.get('editor.linter');
|
2017-08-28 13:33:45 +00:00
|
|
|
const baseUrl = linter === 'stylelint'
|
2017-08-23 22:13:55 +00:00
|
|
|
? 'https://stylelint.io/user-guide/rules/'
|
|
|
|
// some CSSLint rules do not have a url
|
|
|
|
: 'https://github.com/CSSLint/csslint/issues/535';
|
2017-08-28 13:33:45 +00:00
|
|
|
let headerLink, template;
|
2017-08-23 22:13:55 +00:00
|
|
|
if (linter === 'csslint') {
|
2017-08-28 05:22:19 +00:00
|
|
|
const CSSLintRules = CSSLint.getRules();
|
2017-08-28 13:33:45 +00:00
|
|
|
headerLink = makeLink('https://github.com/CSSLint/csslint/wiki/Rules-by-ID', 'CSSLint');
|
2017-08-23 22:13:55 +00:00
|
|
|
template = ruleID => {
|
2017-08-28 13:33:45 +00:00
|
|
|
const rule = CSSLintRules.find(rule => rule.id === ruleID);
|
|
|
|
return rule &&
|
|
|
|
$element({tag: 'li', appendChild: [
|
|
|
|
$element({tag: 'b', appendChild: makeLink(rule.url || baseUrl, rule.name)}),
|
|
|
|
$element({tag: 'br'}),
|
|
|
|
rule.desc,
|
|
|
|
]});
|
2017-08-23 22:13:55 +00:00
|
|
|
};
|
2017-08-16 21:01:45 +00:00
|
|
|
} else {
|
2017-08-28 13:33:45 +00:00
|
|
|
headerLink = makeLink(baseUrl, 'stylelint');
|
|
|
|
template = rule =>
|
|
|
|
$element({
|
|
|
|
tag: 'li',
|
|
|
|
appendChild: makeLink(baseUrl + rule, rule),
|
|
|
|
});
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-28 13:33:45 +00:00
|
|
|
const header = t('linterIssuesHelp', '\x01').split('\x01');
|
|
|
|
const activeRules = new Set($$('#lint td[role="severity"]').map(el => el.dataset.rule));
|
|
|
|
return showHelp(t('linterIssues'),
|
|
|
|
$element({appendChild: [
|
|
|
|
header[0], headerLink, header[1],
|
|
|
|
$element({
|
|
|
|
tag: 'ul',
|
|
|
|
className: 'rules',
|
|
|
|
appendChild: [...activeRules.values()].map(template),
|
|
|
|
}),
|
|
|
|
]})
|
|
|
|
);
|
2017-08-16 21:01:45 +00:00
|
|
|
}
|
2017-08-17 19:08:48 +00:00
|
|
|
|
2017-08-25 20:49:20 +00:00
|
|
|
function showLinterErrorMessage(title, contents) {
|
|
|
|
messageBox({
|
|
|
|
title,
|
|
|
|
contents,
|
|
|
|
className: 'danger center lint-config',
|
|
|
|
buttons: [t('confirmOK')],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-08-23 22:13:55 +00:00
|
|
|
function setupLinterSettingsEvents(popup) {
|
2017-08-20 18:32:41 +00:00
|
|
|
$('.save', popup).addEventListener('click', event => {
|
2017-08-18 15:46:09 +00:00
|
|
|
event.preventDefault();
|
2017-08-28 05:22:19 +00:00
|
|
|
const linter = linterConfig.setLinter(event.target.dataset.linter);
|
2017-08-20 01:49:13 +00:00
|
|
|
const json = tryJSONparse(popup.codebox.getValue());
|
2017-08-26 14:30:33 +00:00
|
|
|
if (json) {
|
2017-08-28 05:22:19 +00:00
|
|
|
const invalid = linterConfig.findInvalidRules(json, linter);
|
2017-08-25 20:49:20 +00:00
|
|
|
if (invalid.length) {
|
2017-08-28 05:22:19 +00:00
|
|
|
showLinterErrorMessage(linter, [
|
|
|
|
t('linterInvalidConfigError'),
|
|
|
|
$element({
|
|
|
|
tag: 'ul',
|
|
|
|
appendChild: invalid.map(name =>
|
|
|
|
$element({tag: 'li', textContent: name})),
|
|
|
|
}),
|
|
|
|
]);
|
|
|
|
return;
|
2017-08-25 20:49:20 +00:00
|
|
|
}
|
2017-08-28 05:22:19 +00:00
|
|
|
linterConfig.save(json);
|
|
|
|
linterConfig.showSavedMessage();
|
2017-08-29 15:21:57 +00:00
|
|
|
popup.codebox.markClean();
|
2017-08-18 15:44:19 +00:00
|
|
|
} else {
|
2017-08-26 16:48:32 +00:00
|
|
|
showLinterErrorMessage(linter, t('linterJSONError'));
|
2017-08-17 19:08:48 +00:00
|
|
|
}
|
2017-08-26 15:46:04 +00:00
|
|
|
popup.codebox.focus();
|
2017-08-17 19:08:48 +00:00
|
|
|
});
|
2017-08-20 18:32:41 +00:00
|
|
|
$('.reset', popup).addEventListener('click', event => {
|
2017-08-18 15:46:09 +00:00
|
|
|
event.preventDefault();
|
2017-08-28 05:22:19 +00:00
|
|
|
const linter = linterConfig.setLinter(event.target.dataset.linter);
|
|
|
|
popup.codebox.setValue(linterConfig.stringify(linterConfig.defaults[linter] || {}));
|
2017-08-26 15:46:04 +00:00
|
|
|
popup.codebox.focus();
|
2017-08-17 19:08:48 +00:00
|
|
|
});
|
2017-08-26 15:03:28 +00:00
|
|
|
$('.cancel', popup).addEventListener('click', event => {
|
|
|
|
event.preventDefault();
|
|
|
|
$('.dismiss').dispatchEvent(new Event('click'));
|
|
|
|
});
|
2017-08-17 19:08:48 +00:00
|
|
|
}
|
|
|
|
|
2017-08-27 17:36:36 +00:00
|
|
|
function setupLinterPopup(config) {
|
2017-08-23 22:13:55 +00:00
|
|
|
const linter = prefs.get('editor.linter');
|
|
|
|
const linterTitle = linter === 'stylelint' ? 'Stylelint' : 'CSSLint';
|
2017-08-29 15:21:57 +00:00
|
|
|
|
2017-08-26 16:03:51 +00:00
|
|
|
function makeButton(className, text, options = {}) {
|
|
|
|
return $element(Object.assign(options, {
|
|
|
|
tag: 'button',
|
|
|
|
className,
|
|
|
|
type: 'button',
|
|
|
|
textContent: t(text),
|
|
|
|
dataset: {linter}
|
|
|
|
}));
|
2017-08-20 01:49:13 +00:00
|
|
|
}
|
|
|
|
function makeLink(url, textContent) {
|
|
|
|
return $element({tag: 'a', target: '_blank', href: url, textContent});
|
|
|
|
}
|
2017-08-29 15:21:57 +00:00
|
|
|
|
|
|
|
const title = t('linterConfigPopupTitle', linterTitle);
|
|
|
|
const contents = $element({
|
2017-08-20 01:49:13 +00:00
|
|
|
appendChild: [
|
|
|
|
$element({
|
|
|
|
tag: 'p',
|
|
|
|
appendChild: [
|
2017-08-26 16:48:32 +00:00
|
|
|
t('linterRulesLink') + ' ',
|
2017-08-23 22:13:55 +00:00
|
|
|
makeLink(
|
|
|
|
linter === 'stylelint'
|
2017-08-27 17:36:36 +00:00
|
|
|
? 'https://stylelint.io/user-guide/rules/'
|
2017-08-23 22:13:55 +00:00
|
|
|
: 'https://github.com/CSSLint/csslint/wiki/Rules-by-ID',
|
|
|
|
linterTitle
|
|
|
|
),
|
2017-08-26 16:48:32 +00:00
|
|
|
linter === 'csslint' ? ' ' + t('linterCSSLintSettings') : ''
|
2017-08-20 01:49:13 +00:00
|
|
|
]
|
|
|
|
}),
|
2017-08-29 15:21:57 +00:00
|
|
|
makeButton('save', 'styleSaveLabel', {disabled: true}),
|
2017-08-26 15:03:28 +00:00
|
|
|
makeButton('cancel', 'confirmCancel'),
|
2017-08-26 16:48:32 +00:00
|
|
|
makeButton('reset', 'genericResetLabel', {title: t('linterResetMessage')}),
|
2017-08-20 01:49:13 +00:00
|
|
|
$element({
|
|
|
|
tag: 'span',
|
2017-08-25 20:49:20 +00:00
|
|
|
className: 'saved-message',
|
|
|
|
textContent: t('genericSavedMessage')
|
2017-08-20 01:49:13 +00:00
|
|
|
})
|
|
|
|
]
|
2017-08-29 15:21:57 +00:00
|
|
|
});
|
|
|
|
const popup = showCodeMirrorPopup(title, contents, {lint: false});
|
|
|
|
contents.parentNode.appendChild(contents);
|
2017-08-20 01:49:13 +00:00
|
|
|
popup.codebox.focus();
|
2017-08-27 17:36:36 +00:00
|
|
|
popup.codebox.setValue(config);
|
2017-08-26 15:29:33 +00:00
|
|
|
popup.codebox.clearHistory();
|
2017-08-29 15:21:57 +00:00
|
|
|
popup.codebox.markClean();
|
|
|
|
popup.codebox.on('change', cm => {
|
|
|
|
$('.save', popup).disabled = cm.isClean();
|
|
|
|
});
|
2017-08-23 22:13:55 +00:00
|
|
|
setupLinterSettingsEvents(popup);
|
2017-09-12 15:19:16 +00:00
|
|
|
loadScript([
|
|
|
|
'/vendor/codemirror/mode/javascript/javascript.js',
|
|
|
|
'/vendor/codemirror/addon/lint/json-lint.js',
|
|
|
|
'/vendor/jsonlint/jsonlint.js'
|
2017-08-29 15:21:57 +00:00
|
|
|
]).then(() => {
|
|
|
|
popup.codebox.setOption('mode', 'application/json');
|
|
|
|
popup.codebox.setOption('lint', 'json');
|
|
|
|
});
|
2017-08-20 01:49:13 +00:00
|
|
|
}
|
2017-08-20 14:06:17 +00:00
|
|
|
|
2017-08-29 14:12:39 +00:00
|
|
|
function loadLinterAssets(name = prefs.get('editor.linter')) {
|
2017-09-13 13:26:22 +00:00
|
|
|
if (!name) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return loadLibrary().then(loadAddon);
|
|
|
|
|
|
|
|
function loadLibrary() {
|
|
|
|
if (name === 'csslint' && !window.CSSLint) {
|
|
|
|
return loadScript([
|
|
|
|
'/vendor-overwrites/csslint/csslint-worker.js',
|
|
|
|
'/edit/lint-defaults-csslint.js'
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
if (name === 'stylelint' && !window.stylelint) {
|
|
|
|
return loadScript([
|
2017-09-12 15:19:16 +00:00
|
|
|
'/vendor-overwrites/stylelint/stylelint-bundle.min.js',
|
|
|
|
'/edit/lint-defaults-stylelint.js'
|
2017-09-13 13:26:22 +00:00
|
|
|
]).then(() => (window.stylelint = require('stylelint')));
|
|
|
|
}
|
|
|
|
return Promise.resolve();
|
2017-08-28 05:22:19 +00:00
|
|
|
}
|
2017-09-13 13:26:22 +00:00
|
|
|
|
|
|
|
function loadAddon() {
|
|
|
|
if ($('script[src$="/vendor/codemirror/addon/lint/lint.js"]')) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
return loadScript([
|
2017-09-12 15:19:16 +00:00
|
|
|
'/vendor/codemirror/addon/lint/lint.css',
|
|
|
|
'/msgbox/msgbox.css',
|
|
|
|
'/vendor/codemirror/addon/lint/lint.js',
|
|
|
|
'/edit/lint-codemirror-helper.js',
|
|
|
|
'/msgbox/msgbox.js'
|
2017-09-13 13:26:22 +00:00
|
|
|
]);
|
2017-08-20 14:06:17 +00:00
|
|
|
}
|
|
|
|
}
|