scribe-plugin-span-style
Advanced tools
Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "scribe-plugin-span-style", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "A Scribe Plugin for adding inline styles through <span> elements", | ||
@@ -5,0 +5,0 @@ "main": "src/scribe-plugin-span-style.js", |
@@ -1,72 +0,76 @@ | ||
module.exports = function(styleName) { | ||
return function(scribe) { | ||
let spanStyleCommand = new scribe.api.SimpleCommand(styleName); | ||
spanStyleCommand.nodeName = 'SPAN'; | ||
define(function () { | ||
'use strict'; | ||
let clearChildStyles = (root) => { | ||
if (typeof root === 'undefined' || typeof root.childNodes === 'undefined') return; | ||
return function (styleName) { | ||
return function(scribe) { | ||
let spanStyleCommand = new scribe.api.SimpleCommand(styleName); | ||
spanStyleCommand.nodeName = 'SPAN'; | ||
for (let i in root.childNodes) { | ||
let child = root.childNodes[i]; | ||
let clearChildStyles = (root) => { | ||
if (typeof root === 'undefined' || typeof root.childNodes === 'undefined') return; | ||
clearChildStyles(child.childNodes); | ||
for (let i in root.childNodes) { | ||
let child = root.childNodes[i]; | ||
if (child.nodeName === 'SPAN') { | ||
child.style[styleName] = ''; | ||
if (!child.getAttribute('style')) { | ||
scribe.node.unwrap(root, child); | ||
clearChildStyles(child.childNodes); | ||
if (child.nodeName === 'SPAN') { | ||
child.style[styleName] = ''; | ||
if (!child.getAttribute('style')) { | ||
scribe.node.unwrap(root, child); | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
}; | ||
spanStyleCommand.execute = function(value) { | ||
scribe.transactionManager.run(() => { | ||
let selection = new scribe.api.Selection(); | ||
let range = selection.range; | ||
spanStyleCommand.execute = function(value) { | ||
scribe.transactionManager.run(() => { | ||
let selection = new scribe.api.Selection(); | ||
let range = selection.range; | ||
// Get Range Text & Current Node Text | ||
let selectedHtmlDocumentFragment = range.extractContents(); | ||
let tDiv = document.createElement('div'); | ||
tDiv.appendChild(selectedHtmlDocumentFragment.cloneNode(true)); | ||
let rangeText = tDiv.innerText; | ||
let nodeText = selection.selection.focusNode.textContent; | ||
// Get Range Text & Current Node Text | ||
let selectedHtmlDocumentFragment = range.extractContents(); | ||
let tDiv = document.createElement('div'); | ||
tDiv.appendChild(selectedHtmlDocumentFragment.cloneNode(true)); | ||
let rangeText = tDiv.innerText; | ||
let nodeText = selection.selection.focusNode.textContent; | ||
// Determine if we need a new node | ||
let isNewNode = true; | ||
if (nodeText === rangeText) { | ||
isNewNode = (selection.selection.focusNode.parentElement.nodeName === 'SPAN') ? false : true; | ||
} | ||
// Determine if we need a new node | ||
let isNewNode = true; | ||
if (nodeText === rangeText) { | ||
isNewNode = (selection.selection.focusNode.parentElement.nodeName === 'SPAN') ? false : true; | ||
} | ||
// Create / Get SPAN | ||
let span = (!isNewNode) ? selection.selection.focusNode.parentElement : document.createElement('span'); | ||
span.appendChild(selectedHtmlDocumentFragment); | ||
if (isNewNode) { | ||
range.insertNode(span); | ||
range.selectNode(span); | ||
} | ||
// Clear Setting for children | ||
clearChildStyles(span); | ||
// Create / Get SPAN | ||
let span = (!isNewNode) ? selection.selection.focusNode.parentElement : document.createElement('span'); | ||
span.appendChild(selectedHtmlDocumentFragment); | ||
if (isNewNode) { | ||
range.insertNode(span); | ||
range.selectNode(span); | ||
} | ||
// Clear Setting for children | ||
clearChildStyles(span); | ||
// Apply new Font-Size | ||
span.style[styleName] = value; | ||
// Apply new Font-Size | ||
span.style[styleName] = value; | ||
// Re-apply the range | ||
selection.selection.removeAllRanges(); | ||
selection.selection.addRange(range); | ||
}); | ||
}; | ||
// Re-apply the range | ||
selection.selection.removeAllRanges(); | ||
selection.selection.addRange(range); | ||
}); | ||
}; | ||
spanStyleCommand.queryState = function() { | ||
var selection = new scribe.api.Selection(); | ||
return !!selection.getContaining(node => { | ||
if (node.style) { | ||
return (node.nodeName === this.nodeName && node.style[styleName] !== ''); | ||
} | ||
return false; | ||
}); | ||
spanStyleCommand.queryState = function() { | ||
var selection = new scribe.api.Selection(); | ||
return !!selection.getContaining(node => { | ||
if (node.style) { | ||
return (node.nodeName === this.nodeName && node.style[styleName] !== ''); | ||
} | ||
return false; | ||
}); | ||
}; | ||
scribe.commands[styleName] = spanStyleCommand; | ||
}; | ||
scribe.commands[styleName] = spanStyleCommand; | ||
}; | ||
}; | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4411
62