2017-08-30 2 views
0

Bonjour d'abord je poste ici cause avoir aucune réponse dans stackoverflow ..quill js embed + ops

Je veux créer embarquer pour les variables. (terminé) Je lance un module qui crée un bouton qui va insérer des variables (terminé) je l'envoie à l'arrière et après m'envoyer des ops de l'ancien texte avec des variables je veux le réinsérer dans l'éditeur. Mais quand j'ai undefined veux re créer Embed ** ** Reproduire

cliquez sur un bouton prenom ou cool cliquez sur le bouton me montrer , il efface et essayer de nouveau créer la table ops dans l'éditeur

\t console.log(Quill.version); 
 
\t window.document.querySelector('.show') 
 
    \t .addEventListener('click', function(){ 
 
\t \t var ops = quill.getContents().ops; 
 
\t \t var _quill = quill; 
 
\t \t setTimeout(function(){ 
 
\t \t \t _quill.deleteText(0, 10); 
 
\t \t }, 3000); 
 
\t \t setTimeout(function(){ 
 
\t \t \t _quill.setContents(ops); 
 
\t \t }, 4000); 
 
\t }); 
 
    
 
    var Variables = function(quill, options){ 
 
\t \t this.quill = quill; 
 
\t \t this.options = options; 
 
\t \t var _this = this; 
 
\t } 
 

 
\t Variables.prototype.initInsertVariable = function(variables, lang, container){ 
 
\t \t var _this = this; 
 
\t \t variables.variables.forEach(function(elem){ 
 
\t \t  var btn = document.createElement("BUTTON"); 
 
\t \t  var delim1 = document.createElement('span'); 
 
\t \t  delim1.classList.add('delimiter'); 
 
\t \t  delim1.appendChild(document.createTextNode('>')); 
 
\t \t  var delim2 = document.createElement('span'); 
 
\t \t  delim2.appendChild(document.createTextNode('<')); 
 
\t \t  delim2.classList.add('delimiter'); 
 
\t \t  btn.appendChild(delim2); 
 
\t \t  var printedMe = elem.printed[lang].replace(/</g,'').replace(/>/g,''); 
 
\t \t  var text = document.createTextNode(printedMe); 
 
\t \t  btn.appendChild(text); 
 
\t \t  btn.appendChild(delim1); 
 
\t \t  btn.classList.add('tool-button'); 
 
\t \t  btn.classList.add('variables'); 
 
\t \t  btn.addEventListener('click', function(){ 
 
\t \t  _this.quill.insertEmbed(0, 'variable', { 
 
\t \t  \t value: printedMe, 
 
\t \t  \t key: elem.key 
 
\t \t  }); 
 
\t \t  // _this.quill.setSelection(_this.indexStory + 1); 
 
\t \t  // _this.quill.focus(); 
 
\t \t  }); 
 
\t \t  container.appendChild(btn); 
 
\t \t }); 
 
\t }; 
 

 
\t Quill.register('modules/variables', Variables); 
 

 

 
\t var Embed = Quill.import('blots/embed'); 
 

 
\t class variable extends Embed { 
 

 
\t \t static create(value) { 
 
\t \t  let node = super.create(); 
 
\t \t  node.setAttribute('keyValue', value.key); 
 
\t \t  node.innerHTML = value.value; 
 
\t \t  // Sanitize url value if desired 
 
\t \t  // node.setAttribute('href', value); 
 
\t \t  // Okay to set other non-format related attributes 
 
\t \t  // These are invisible to Parchment so must be static 
 
\t \t  // node.setAttribute('target', '_blank'); 
 
\t \t  return node; 
 
\t \t } 
 

 
\t \t static formats(node) { 
 
\t \t \t return node.getAttribute('keyValue'); 
 
\t \t \t // We will only be called with a node already 
 
\t \t \t // determined to be a Link blot, so we do 
 
\t \t \t // not need to check ourselves 
 
\t \t \t // return node.getAttribute('href'); 
 
\t \t } 
 
\t }; 
 

 

 
\t variable.blotName = 'variable'; 
 
\t variable.tagName = 'variable'; 
 

 
\t Quill.register({ 
 
\t \t 'formats/variable': variable 
 
\t }); 
 

 

 

 

 
\t var Delta = Quill.import('delta'); 
 

 
\t var quill = new Quill('#editor', { 
 
\t \t // debug: 'info', 
 
\t \t modules: { 
 
\t \t \t variables: true, 
 
\t \t \t history: { 
 
\t \t \t delay: 2000, 
 
\t \t \t maxStack: 500, 
 
\t \t \t userOnly: true 
 
\t \t \t }, 
 
\t \t \t toolbar: '#toolbar' 
 
\t \t }, 
 
\t \t theme: 'snow', 
 
\t }); 
 

 
    var variables = quill.getModule('variables'); 
 

 
\t // vars mock from back 
 
\t var Vars = { 
 
\t \t variables : [{ 
 
\t \t \t key: '*|FNAME|*', 
 
\t \t \t printed: { 
 
\t \t \t \t 'fr': '<Prenom>' 
 
\t \t \t } 
 
\t \t }, 
 
\t \t { 
 
\t \t \t key: '*|COOL|*', 
 
\t \t \t printed: { 
 
\t \t \t \t 'fr': '<Cool>' 
 
\t \t \t } 
 
\t \t }] 
 
\t }; 
 
\t variables.initInsertVariable(Vars, 'fr', window.document.querySelector('#variables')); 
 

 
\t quill.clipboard.addMatcher('.editor-variables', function(node, delta) { 
 
\t \t return delta.compose(new Delta().retain(delta.length(), { 
 
\t \t \t bold: true, 
 
\t \t \t backgroundColor: '#ff0000' 
 
\t \t })); 
 
\t });
.editor{ 
 
\t width: 400px; 
 
\t height: 400px; 
 
\t background-color: grey; 
 
}
<link href="//cdn.quilljs.com/1.3.1/quill.snow.css" rel="stylesheet"/> 
 
<script src="//cdn.quilljs.com/1.3.1/quill.js"></script> 
 
<body> 
 
\t <div id="variables"></div> 
 
\t <div id="toolbar"> 
 
\t  <button class="ql-bold"></button> 
 
\t  <button class="ql-italic"></button> 
 
\t  <button class="ql-underline"></button> 
 
\t  <button class="ql-list" value="bullet"></button> 
 
\t  <button class="ql-list" value="ordered"></button> 
 
\t  <button class="ql-indent" value="-1"></button> 
 
\t  <button class="ql-indent" value="+1"></button> 
 
\t  <button class="ql-link" ></button> 
 
\t </div> 
 
\t <div id="editor"> 
 
\t </div> 
 

 
\t <button type="button" class="show">show me</button> 
 
</body>

comportement attendu:

quand je re insérer les opérations avec Embed. Il devrait me montrer le même

Comportement réel:

me montrer indéfinis

Plates-formes:

Inclure navigateur, système d'exploitation et les versions respectives

Version:

1.3. 1

Répondre

1

Je suis Vous voulez comme ça.

console.log(Quill.version); 
 
\t window.document.querySelector('.show') 
 
    \t .addEventListener('click', function(){ 
 
\t \t ops = document.querySelector(".ql-editor").innerHTML; 
 
\t \t var _quill = quill; 
 
\t \t setTimeout(function(){ 
 
\t \t \t _quill.deleteText(0, 10); 
 
\t \t }, 3000); 
 
\t \t setTimeout(function(){ 
 
\t \t \t document.querySelector(".ql-editor").innerHTML = ops; 
 
\t \t }, 4000); 
 
\t }); 
 
    
 
    var Variables = function(quill, options){ 
 
\t \t this.quill = quill; 
 
\t \t this.options = options; 
 
\t \t var _this = this; 
 
\t } 
 

 
\t Variables.prototype.initInsertVariable = function(variables, lang, container){ 
 
\t \t var _this = this; 
 
\t \t variables.variables.forEach(function(elem){ 
 
\t \t  var btn = document.createElement("BUTTON"); 
 
\t \t  var delim1 = document.createElement('span'); 
 
\t \t  delim1.classList.add('delimiter'); 
 
\t \t  delim1.appendChild(document.createTextNode('>')); 
 
\t \t  var delim2 = document.createElement('span'); 
 
\t \t  delim2.appendChild(document.createTextNode('<')); 
 
\t \t  delim2.classList.add('delimiter'); 
 
\t \t  btn.appendChild(delim2); 
 
\t \t  var printedMe = elem.printed[lang].replace(/</g,'').replace(/>/g,''); 
 
\t \t  var text = document.createTextNode(printedMe); 
 
\t \t  btn.appendChild(text); 
 
\t \t  btn.appendChild(delim1); 
 
\t \t  btn.classList.add('tool-button'); 
 
\t \t  btn.classList.add('variables'); 
 
\t \t  btn.addEventListener('click', function(){ 
 
\t \t  _this.quill.insertEmbed(0, 'variable', { 
 
\t \t  \t value: printedMe, 
 
\t \t  \t key: elem.key 
 
\t \t  }); 
 
\t \t  // _this.quill.setSelection(_this.indexStory + 1); 
 
\t \t  // _this.quill.focus(); 
 
\t \t  }); 
 
\t \t  container.appendChild(btn); 
 
\t \t }); 
 
\t }; 
 

 
\t Quill.register('modules/variables', Variables); 
 

 

 
\t var Embed = Quill.import('blots/embed'); 
 

 
\t class variable extends Embed { 
 

 
\t \t static create(value) { 
 
\t \t  let node = super.create(); 
 
\t \t  node.setAttribute('keyValue', value.key); 
 
\t \t  node.innerHTML = value.value; 
 
\t \t  // Sanitize url value if desired 
 
\t \t  // node.setAttribute('href', value); 
 
\t \t  // Okay to set other non-format related attributes 
 
\t \t  // These are invisible to Parchment so must be static 
 
\t \t  // node.setAttribute('target', '_blank'); 
 
\t \t  return node; 
 
\t \t } 
 

 
\t \t static formats(node) { 
 
\t \t \t return node.getAttribute('keyValue'); 
 
\t \t \t // We will only be called with a node already 
 
\t \t \t // determined to be a Link blot, so we do 
 
\t \t \t // not need to check ourselves 
 
\t \t \t // return node.getAttribute('href'); 
 
\t \t } 
 
\t }; 
 

 

 
\t variable.blotName = 'variable'; 
 
\t variable.tagName = 'variable'; 
 

 
\t Quill.register({ 
 
\t \t 'formats/variable': variable 
 
\t }); 
 

 

 

 

 
\t var Delta = Quill.import('delta'); 
 

 
\t var quill = new Quill('#editor', { 
 
\t \t // debug: 'info', 
 
\t \t modules: { 
 
\t \t \t variables: true, 
 
\t \t \t history: { 
 
\t \t \t delay: 2000, 
 
\t \t \t maxStack: 500, 
 
\t \t \t userOnly: true 
 
\t \t \t }, 
 
\t \t \t toolbar: '#toolbar' 
 
\t \t }, 
 
\t \t theme: 'snow', 
 
\t }); 
 

 
    var variables = quill.getModule('variables'); 
 

 
\t // vars mock from back 
 
\t var Vars = { 
 
\t \t variables : [{ 
 
\t \t \t key: '*|FNAME|*', 
 
\t \t \t printed: { 
 
\t \t \t \t 'fr': '<Prenom>' 
 
\t \t \t } 
 
\t \t }, 
 
\t \t { 
 
\t \t \t key: '*|COOL|*', 
 
\t \t \t printed: { 
 
\t \t \t \t 'fr': '<Cool>' 
 
\t \t \t } 
 
\t \t }] 
 
\t }; 
 
\t variables.initInsertVariable(Vars, 'fr', window.document.querySelector('#variables')); 
 

 
\t quill.clipboard.addMatcher('.editor-variables', function(node, delta) { 
 
\t \t return delta.compose(new Delta().retain(delta.length(), { 
 
\t \t \t bold: true, 
 
\t \t \t backgroundColor: '#ff0000' 
 
\t \t })); 
 
\t });
.editor{ 
 
\t width: 400px; 
 
\t height: 400px; 
 
\t background-color: grey; 
 
}
<link href="//cdn.quilljs.com/1.3.1/quill.snow.css" rel="stylesheet"/> 
 
<script src="//cdn.quilljs.com/1.3.1/quill.js"></script> 
 
<body> 
 
\t <div id="variables"></div> 
 
\t <div id="toolbar"> 
 
\t  <button class="ql-bold"></button> 
 
\t  <button class="ql-italic"></button> 
 
\t  <button class="ql-underline"></button> 
 
\t  <button class="ql-list" value="bullet"></button> 
 
\t  <button class="ql-list" value="ordered"></button> 
 
\t  <button class="ql-indent" value="-1"></button> 
 
\t  <button class="ql-indent" value="+1"></button> 
 
\t  <button class="ql-link" ></button> 
 
\t </div> 
 
\t <div id="editor"> 
 
\t </div> 
 

 
\t <button type="button" class="show">show me</button> 
 
</body>

Vérifiez cela et laissez-moi savoir?

+0

je vous écris deux answears merci pour votre temps –