Hi All!
I'm attempting to determine the best route method to use to integrate Scribe and can.Component.
A wee bit of context. Scribe wraps a raw element, either a fragment, or a node inserted into the Dom which has it's contenteditable attr set to true, with normalizing logic to clear up cross browser inconsistencies. However, due to the way it works live binding doesn't work.
The approch I'm am currently taking is to use can.view.tag to define a tag and wrap it with scribe. However, I am encountering some pitfalls. The can.view.tag api yields the raw mustache/stash context, not a context based on the attributes passed in. This means I have duplicated code likely already present in the canjs code base.
My goal here is to collect feedback on how best to implement this component.
Here is my current attempt.
I'm attempting to determine the best route method to use to integrate Scribe and can.Component.
A wee bit of context. Scribe wraps a raw element, either a fragment, or a node inserted into the Dom which has it's contenteditable attr set to true, with normalizing logic to clear up cross browser inconsistencies. However, due to the way it works live binding doesn't work.
The approch I'm am currently taking is to use can.view.tag to define a tag and wrap it with scribe. However, I am encountering some pitfalls. The can.view.tag api yields the raw mustache/stash context, not a context based on the attributes passed in. This means I have duplicated code likely already present in the canjs code base.
My goal here is to collect feedback on how best to implement this component.
Here is my current attempt.
- define(
- ['can', '../viewModels/scribe-editor','scribe'],
- function(can, ScribeEditorVM, Scribe)
- {
- var commands = ['createLink'],
- attributes = ['content'];
- function getValidValue(map, name){
- if(name[0] === '{' && name[name.length-1] === '}'){
- return map.attr(name.substr(1, name.length-2));
- } else {
- return map.attr(name);
- }
- }
- can.view.tag('scribe-editor', function(el, context){
- var vm = new ScribeEditorVM(), scribe = new Scribe(el);
- attributes.forEach(function(attrName){
- var attr = el.getAttribute(attrName);
- // Specifically this feels like duplication of code, is there a better way?
- vm.attr(attrName, getValidValue(context.scope, attr));
- });
- scribe.setContent(vm.attr('content'));
- vm.on('change', function(){
- console.log('context changed');
- console.log(arguments);
- })
- function checkCommandState(commandName){
- var selection = new scribe.api.Selection(),
- command = scribe.getCommand(commandName);
- if(selection.range && command.queryEnabled()){
- scope.attr(commandName, command.queryState());
- }
- }
- scribe.on('content-changed', function(){
- vm.attr('content', scribe.getContent());
- commands.forEach(checkCommandState);
- });
- })
- });