-
Notifications
You must be signed in to change notification settings - Fork 503
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Works like -ko-bind-text but is specifically thought for <style> elements and automatically tries to restrict the rules to the wysiwyg editing area.
- Loading branch information
Showing
3 changed files
with
64 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
"use strict"; | ||
var ko = require("knockout"); | ||
var console = require("console"); | ||
|
||
var cssParse = require("mensch/lib/parser.js"); | ||
var converterUtils = require("../converter/utils.js"); | ||
|
||
var _processStylesheetRules = function(style, rules) { | ||
var newStyle = style; | ||
var lastStart = null; | ||
|
||
if (typeof rules == 'undefined') { | ||
var styleSheet = cssParse(style, { | ||
comments: true, | ||
position: true | ||
}); | ||
if (styleSheet.type != 'stylesheet' || typeof styleSheet.stylesheet == 'undefined') { | ||
console.log("unable to process styleSheet", styleSheet); | ||
throw "Unable to parse stylesheet"; | ||
} | ||
rules = styleSheet.stylesheet.rules; | ||
} | ||
|
||
for (var i = rules.length - 1; i >= 0; i--) { | ||
if (rules[i].type == 'media' || rules[i].type == 'supports') { | ||
newStyle = _processStylesheetRules(newStyle, rules[i].rules); | ||
} else if (rules[i].type == 'comment') { | ||
// ignore comments | ||
} else if (rules[i].type == 'rule') { | ||
var sels = rules[i].selectors; | ||
var newSel = ""; | ||
for (var j = 0; j < sels.length; j++) { | ||
if (newSel.length > 0) newSel += ", "; | ||
newSel += '#main-wysiwyg-area ' + sels[j]; | ||
} | ||
newStyle = converterUtils.removeStyle(newStyle, rules[i].position.start, rules[i].position.end, 0, 0, 0, newSel); | ||
} else { | ||
console.error("Unsupported rule type", rules[i].type, "while parsing <style> rules"); | ||
} | ||
lastStart = rules[i].position.start; | ||
} | ||
return newStyle; | ||
}; | ||
|
||
|
||
ko.bindingHandlers.stylesheet = { | ||
_makeComputedValueAccessor: function(valueAccessor) { | ||
return function() { | ||
var inputCss = ko.utils.unwrapObservable(valueAccessor()); | ||
return _processStylesheetRules(inputCss); | ||
}; | ||
}, | ||
init: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | ||
return ko.bindingHandlers['text'].init(); | ||
}, | ||
update: function(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext) { | ||
var isWysiwygMode = (typeof bindingContext.templateMode !== 'undefined' && bindingContext.templateMode == 'wysiwyg'); | ||
if (isWysiwygMode) valueAccessor = ko.bindingHandlers.stylesheet._makeComputedValueAccessor(valueAccessor); | ||
return ko.bindingHandlers['text'].update(element, valueAccessor, allBindingsAccessor, viewModel, bindingContext); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters