Switch to new linter/worker

This commit is contained in:
eight 2018-09-01 19:12:24 +08:00
parent 054d94ebd2
commit ca5c0e92cd
2 changed files with 11 additions and 29 deletions

View File

@ -6,7 +6,7 @@ global setupCodeMirror
global beautify global beautify
global initWithSectionStyle addSections removeSection getSectionsHashes global initWithSectionStyle addSections removeSection getSectionsHashes
global sectionsToMozFormat global sectionsToMozFormat
global moveFocus global moveFocus editorWorker
*/ */
'use strict'; 'use strict';
@ -211,7 +211,6 @@ function beforeUnload() {
} }
const isDirty = editor ? editor.isDirty() : !isCleanGlobal(); const isDirty = editor ? editor.isDirty() : !isCleanGlobal();
if (isDirty) { if (isDirty) {
updateLintReportIfEnabled(null, 0);
// neither confirm() nor custom messages work in modern browsers but just in case // neither confirm() nor custom messages work in modern browsers but just in case
return t('styleChangesNotSaved'); return t('styleChangesNotSaved');
} }
@ -275,9 +274,6 @@ function initHooks() {
$('#save-button').addEventListener('click', save, false); $('#save-button').addEventListener('click', save, false);
$('#sections-help').addEventListener('click', showSectionHelp, false); $('#sections-help').addEventListener('click', showSectionHelp, false);
// TODO: investigate why FF needs this delay
debounce(initLint, FIREFOX ? 100 : 0);
if (!FIREFOX) { if (!FIREFOX) {
$$([ $$([
'input:not([type])', 'input:not([type])',
@ -352,7 +348,6 @@ function toggleStyle() {
} }
function save() { function save() {
updateLintReportIfEnabled(null, 0);
if (!validate()) { if (!validate()) {
return; return;
} }
@ -413,12 +408,6 @@ function updateTitle() {
$('#save-button').disabled = clean; $('#save-button').disabled = clean;
} }
function updateLintReportIfEnabled(...args) {
if (CodeMirror.defaults.lint) {
updateLintReport(...args);
}
}
function showMozillaFormat() { function showMozillaFormat() {
const popup = showCodeMirrorPopup(t('styleToMozillaFormatTitle'), '', {readOnly: true}); const popup = showCodeMirrorPopup(t('styleToMozillaFormatTitle'), '', {readOnly: true});
popup.codebox.setValue(toMozillaFormat()); popup.codebox.setValue(toMozillaFormat());
@ -460,16 +449,7 @@ function fromMozillaFormat() {
function doImport({replaceOldStyle = false}) { function doImport({replaceOldStyle = false}) {
lockPageUI(true); lockPageUI(true);
new Promise(setTimeout) editorWorker.parseMozFormat(popup.codebox.getValue().trim())
.then(() => {
const worker = linterConfig.worker.csslint;
if (!worker.instance) worker.instance = new Worker(worker.path);
})
.then(() => linterConfig.invokeWorker({
linter: 'csslint',
action: 'parse',
code: popup.codebox.getValue().trim(),
}))
.then(({sections, errors}) => { .then(({sections, errors}) => {
// shouldn't happen but just in case // shouldn't happen but just in case
if (!sections.length && errors.length) { if (!sections.length && errors.length) {
@ -482,8 +462,7 @@ function fromMozillaFormat() {
removeOldSections(replaceOldStyle); removeOldSections(replaceOldStyle);
return addSections(sections, div => setCleanItem(div, false)); return addSections(sections, div => setCleanItem(div, false));
}) })
.then(sectionDivs => { .then(() => {
sectionDivs.forEach(div => updateLintReportIfEnabled(div.CodeMirror, 1));
$('.dismiss').dispatchEvent(new Event('click')); $('.dismiss').dispatchEvent(new Event('click'));
}) })
.catch(showError) .catch(showError)

View File

@ -1,11 +1,11 @@
/* /*
global CodeMirror global CodeMirror
global editors propertyToCss CssToProperty global editors propertyToCss CssToProperty
global onChange indicateCodeChange initHooks setCleanGlobal global onChange initHooks setCleanGlobal
global fromMozillaFormat maximizeCodeHeight toggleContextMenuDelete global fromMozillaFormat maximizeCodeHeight toggleContextMenuDelete
global setCleanItem updateTitle global setCleanItem updateTitle
global showAppliesToHelp beautify regExpTester setGlobalProgress setCleanSection global showAppliesToHelp beautify regExpTester setGlobalProgress setCleanSection
global clipString global clipString linter linterReport
*/ */
'use strict'; 'use strict';
@ -146,12 +146,13 @@ function addSection(event, section) {
const newIndex = getSections().indexOf(clickedSection) + 1; const newIndex = getSections().indexOf(clickedSection) + 1;
cm = setupCodeMirror(div, code, newIndex); cm = setupCodeMirror(div, code, newIndex);
makeSectionVisible(cm); makeSectionVisible(cm);
renderLintReport();
cm.focus(); cm.focus();
} else { } else {
sections.appendChild(div); sections.appendChild(div);
cm = setupCodeMirror(div, code); cm = setupCodeMirror(div, code);
} }
linter.hook(cm);
linterReport.refresh();
div.CodeMirror = cm; div.CodeMirror = cm;
setCleanSection(div); setCleanSection(div);
return div; return div;
@ -308,7 +309,6 @@ function indicateCodeChange(cm) {
const section = cm.getSection(); const section = cm.getSection();
setCleanItem(section, cm.isClean(section.savedValue)); setCleanItem(section, cm.isClean(section.savedValue));
updateTitle(); updateTitle();
updateLintReportIfEnabled(cm);
} }
function setupAutocomplete(cm, enable = true) { function setupAutocomplete(cm, enable = true) {
@ -481,13 +481,16 @@ function removeSection(event) {
setCleanItem(section, false); setCleanItem(section, false);
updateTitle(); updateTitle();
cm.focus(); cm.focus();
linter.hook(cm);
linterReport.refresh();
}; };
section.insertAdjacentElement('afterend', stub); section.insertAdjacentElement('afterend', stub);
} }
setCleanItem($('#sections'), false); setCleanItem($('#sections'), false);
removeAreaAndSetDirty(section); removeAreaAndSetDirty(section);
editors.splice(editors.indexOf(cm), 1); editors.splice(editors.indexOf(cm), 1);
renderLintReport(); linter.unhook(cm);
linterReport.refresh();
} }
function removeAreaAndSetDirty(area) { function removeAreaAndSetDirty(area) {