Add option to apply surround across multilines.

This commit is contained in:
Guo Xiang Tan 2016-10-14 13:39:00 +08:00
parent becff2de4d
commit 6b40f2c42c
2 changed files with 15 additions and 4 deletions

View File

@ -441,7 +441,7 @@ export default Ember.Component.extend({
}).join("\n"); }).join("\n");
}, },
_applySurround(sel, head, tail, exampleKey) { _applySurround(sel, head, tail, exampleKey, opts) {
const pre = sel.pre; const pre = sel.pre;
const post = sel.post; const post = sel.post;
@ -453,6 +453,16 @@ export default Ember.Component.extend({
const example = I18n.t(`composer.${exampleKey}`); const example = I18n.t(`composer.${exampleKey}`);
this.set('value', `${pre}${hval}${example}${tail}${post}`); this.set('value', `${pre}${hval}${example}${tail}${post}`);
this._selectText(pre.length + hlen, example.length); this._selectText(pre.length + hlen, example.length);
} else if (opts && !opts.multiline) {
const [hval, hlen] = getHead(head);
if (pre.slice(-hlen) === hval && post.slice(0, tail.length) === tail) {
this.set('value', `${pre.slice(0, -hlen)}${sel.value}${post.slice(tail.length)}`);
this._selectText(sel.start - hlen, sel.value.length);
} else {
this.set('value', `${pre}${hval}${sel.value}${tail}${post}`);
this._selectText(sel.start + hlen, sel.value.length);
}
} else { } else {
const lines = sel.value.split("\n"); const lines = sel.value.split("\n");
@ -521,7 +531,7 @@ export default Ember.Component.extend({
const toolbarEvent = { const toolbarEvent = {
selected, selected,
selectText: (from, length) => this._selectText(from, length), selectText: (from, length) => this._selectText(from, length),
applySurround: (head, tail, exampleKey) => this._applySurround(selected, head, tail, exampleKey), applySurround: (head, tail, exampleKey, opts) => this._applySurround(selected, head, tail, exampleKey, opts),
applyList: (head, exampleKey) => this._applyList(selected, head, exampleKey), applyList: (head, exampleKey) => this._applyList(selected, head, exampleKey),
addText: text => this._addText(selected, text), addText: text => this._addText(selected, text),
getText: () => this.get('value'), getText: () => this.get('value'),

View File

@ -18,8 +18,9 @@ function initializeDetails(api) {
this.get("toolbarEvent").applySurround( this.get("toolbarEvent").applySurround(
`[details=${I18n.t("composer.details_title")}]`, `[details=${I18n.t("composer.details_title")}]`,
"[/details]", "[/details]",
"details_text") "details_text",
; { multiline: false }
);
} }
} }
}); });