2016-06-13 10 views
4

我正在研究使用Quill進行項目,並且我需要知道是否可以創建比單個元素和單個參數更復雜的自定義格式/印跡。是否可以創建具有複雜子結構的自定義格式/印跡?

我想要的佈局之一的一個例子是:

<span class="format-container"> 
    <span class="format-info" data-attr="param 1 (non-displayed)"> 
     param 2 (displayed to user -- click to invoke application UI to edit) 
    </span> 
    <span class="format-content"> 
     User's text/child elements go here 
    </span> 
</span> 

在我尋找到所有的情況下,自定義格式是內嵌範圍,仍然有一個父容器和一個地方爲孩子的內容去。

Quill中的自定義格式目前似乎沒有很好的記錄。我在消息來源中發現,並能夠發現在0.20.1中這很可能是不可能的。但是,我覺得在1.0.0 beta w/Parchment中可以做到這一點,我只是不確定我實際需要寫什麼。

那麼這是可能的在1.0.0?如果是這樣,那該怎麼辦?

編輯:這就是我要爲:Example

+0

有趣的是我幾乎完全一樣的要求。我嘗試了一個Embed,但它總是強迫我的內容進入自己的新生產線,這真讓人氣憤。你有沒有想過這個? –

+0

我花了很多時間在圖書館的源代碼中進行挖掘,實際上我認爲這不可能。儘管羊皮紙是一個DOM「抽象」,但它仍然依賴於DOM中的父/子關係,這意味着像這樣的子容器中的子元素可能會破壞事物。最後,我剛剛重新設計了自己的格式化系統,因此沒有必要。 –

回答

6

所以,我想通過最小的努力最終做到這一點。它涉及到爲Quill 1.3或更高版本定義新的印跡類型,相同的代碼應該在舊版本上工作,但是未經測試。

查看工作示例的代碼片段。關鍵是要擴展現有的Embed blot'blots/embed',並定義自己的工具欄處理程序以注入任意DOM節點實例。

// utility function used to inherit non prototypical methods/properties 
 
function extend(target, base) { 
 
    for (var prop in base) { 
 
    target[prop] = base[prop]; 
 
    } 
 
} 
 

 
// definition of a custom Blot. 
 
(function(Embed) { 
 
    'use strict'; 
 

 
    function Span() { 
 
    Object.getPrototypeOf(Embed).apply(this, arguments); 
 
    } 
 

 
    Span.prototype = Object.create(Embed && Embed.prototype); 
 
    Span.prototype.constructor = Span; 
 
    extend(Span, Embed); 
 

 
    Span.create = function create(value) { 
 
    return value; // expects a domNode as value 
 
    }; 
 

 
    Span.value = function value(domNode) { 
 
    return domNode; 
 
    }; 
 

 
    Span.blotName = 'span'; 
 
    Span.tagName = 'SPAN'; 
 
    Span.className = 'complex'; 
 

 
    Quill.register(Span, true); 
 
})(Quill.import('blots/embed')); // import the embed blot. This is important as this is being extended 
 

 
// custom handler for the toolbar button 
 
var handler = function() { 
 
    var complexSpan = document.getElementById('complextype').firstElementChild.cloneNode(true); 
 
    var selection = quill.getSelection(); 
 

 
    quill.insertEmbed(selection.index, 'span', complexSpan); 
 
} 
 

 
// instantiate quill. Note that modules.toolbar.handlers has a 'span' handler. Quill parses this from // the class on the button in the toolbar markup: 'ql-span' this is 'ql-' + blotName 
 
var quill = new Quill('#editor', { 
 
    modules: { 
 
    toolbar: { 
 
     container: '.toolbar', 
 
     handlers: { 
 
     'span': handler 
 
     } 
 
    } 
 
    }, 
 
    theme: 'snow' 
 
});
button.ql-span { 
 
    width: 150px !important; 
 
} 
 

 
.complex { 
 
    border-radius: 10px; 
 
    border: 2px solid black; 
 
    margin: 3px; 
 
} 
 

 
.inner { 
 
    border-radius: 10px; 
 
    background: #767676; 
 
    color: #FFFFFF; 
 
    padding-left: 6px; 
 
    padding-right: 6px; 
 
} 
 

 
.formatting { 
 
    font-weight: bold; 
 
    font-style: italic; 
 
    text-decoration: underline; 
 
} 
 

 
.nested { 
 
    margin-left: 3px; 
 
    margin-right: 3px; 
 
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/quill/1.3.1/quill.min.js"></script> 
 
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/quill/1.3.1/quill.snow.css" /> 
 
<div id="wrapper"> 
 
    <div id="toolbar" class="toolbar"> 
 
    <span class="ql-formats"> 
 
     <button class="ql-bold"></button> 
 
     <button class="ql-italic"></button> 
 
     <button class="ql-underline"></button> 
 
    </span> 
 
    <span class="ql-formats"> 
 
     <button class="ql-span">Complex Span Type</button> 
 
    </span> 
 
    </div> 
 

 
    <div id="editor">Lorem Ipsum 
 
    <span contenteditable="false"> 
 
     <span class="complex" spellcheck="false"> 
 
     <span class="inner">Format applied</span> 
 
     <span class="nested">More text</span> 
 
     <span class="formatting">with formatting</span> 
 
     <span class="nested">dolor</span> 
 
     </span> 
 
    </span> sit amet 
 
    </div> 
 
</div> 
 

 
<div id="complextype" style="display:none;"> 
 
<span contenteditable="false"><span class="complex" spellcheck="false"><span class="inner">Format applied</span><span class="nested">More text</span><span class="formatting">with formatting</span><span class="nested">dolor</span></span></span> 
 
</div>

+0

有趣。我承認我最終完全轉向了一個不同的RTE框架,所以我不能直接測試這個。我不記得在挖掘時看到「處理程序」。所以無論是最近添加的還是我只是錯過了它。很高興你有這個工作!如果你能給我一個Plunkr/JsFiddle /一些其他的工作例子,我會高興地接受這個答案,以便其他人可以更容易地找到它。 –

+0

@StephenCohen我編輯了我的答案並提供了功能性代碼片段。公平地說,缺少定製處理程序並不困難,我不得不花費幾個小時才能解決問題。奎爾是一個偉大的項目,但文檔在各個地方都很少見。 –

+0

不錯!我可以看到它仍然不完美(如果您允許用戶在該範圍內放置任意內容,則在應用該格式後將無法編輯),但它確實在技術上做了我正在嘗試做的事情, ,所以這是你的綠色複選標記! –

0

文檔和指南仍然被寫入,但一個好地方,看是現有的自定義格式是如何實現的。公式格式尤其與您的用例非常相似。

+0

我看了看,發現有重要的不同。其中最大的是這個公式是一個Embed,而我需要一個Inline Blot,它可以有正常的子內容。基本上,外部跨度呈現一個邊界,第一個子跨度呈現一些元數據文本,然後用戶繼續鍵入輸入進入第二個子跨度。我將用可能更清晰的樣機圖像編輯我的問題。 –

相關問題