CSSPreprocessorDocument manages a single LESS or SASS source document
HIGHLIGHTING
CSSPreprocessorDocument supports highlighting all DOMNode corresponding to the rule at the cursor position in the editor.
var CSSPreprocessorDocument = function CSSPreprocessorDocument(doc, editor) {
this.doc = doc;
this.onCursorActivity = this.onCursorActivity.bind(this);
// Add a ref to the doc since we're listening for change events
this.doc.addRef();
this.onActiveEditorChange = this.onActiveEditorChange.bind(this);
EditorManager.on("activeEditorChange", this.onActiveEditorChange);
this.onActiveEditorChange(null, EditorManager.getActiveEditor(), null);
};
// CSSPreprocessorDocument doesn't dispatch events, but the "live document" interface requires an on() API
EventDispatcher.makeEventDispatcher(CSSPreprocessorDocument.prototype);
Close the document
CSSPreprocessorDocument.prototype.close = function close() {
this.doc.off(".CSSPreprocessorDocument");
EditorManager.off("activeEditorChange", this.onActiveEditorChange);
this.doc.releaseRef();
this.detachFromEditor();
};
Return false so edits cause "out of sync" icon to appear
CSSPreprocessorDocument.prototype.isLiveEditingEnabled = function () {
// Normally this isn't called since wasURLRequested() returns false for us, but if user's
// page uses less.js to dynamically load LESS files, then it'll be true and we'll get called.
return false;
};
CSSPreprocessorDocument.prototype.attachToEditor = function (editor) {
this.editor = editor;
if (this.editor) {
this.editor.on("cursorActivity.CSSPreprocessorDocument", this.onCursorActivity);
this.updateHighlight();
}
};
CSSPreprocessorDocument.prototype.detachFromEditor = function () {
if (this.editor) {
HighlightAgent.hide();
this.editor.off(".CSSPreprocessorDocument");
this.editor = null;
}
};
CSSPreprocessorDocument.prototype.updateHighlight = function () {
if (Inspector.config.highlight && this.editor) {
var editor = this.editor,
selectors = [];
_.each(this.editor.getSelections(), function (sel) {
var selector = CSSUtils.findSelectorAtDocumentPos(editor, (sel.reversed ? sel.end : sel.start));
if (selector) {
selectors.push(selector);
}
});
if (selectors.length) {
HighlightAgent.rule(selectors.join(","));
} else {
HighlightAgent.hide();
}
}
};
Triggered when the active editor changes
CSSPreprocessorDocument.prototype.onActiveEditorChange = function (event, newActive, oldActive) {
this.detachFromEditor();
if (newActive && newActive.document === this.doc) {
this.attachToEditor(newActive);
}
};
// Export the class
module.exports = CSSPreprocessorDocument;
});