FEATURE: new site setting 'code formatting style'

This commit is contained in:
Arpit Jalan 2016-06-27 00:00:45 +05:30
parent 800081f606
commit 83309752ae
5 changed files with 26 additions and 12 deletions

View File

@ -63,18 +63,7 @@ class Toolbar {
perform: e => e.applySurround('> ', '', 'code_text') perform: e => e.applySurround('> ', '', 'code_text')
}); });
this.addButton({ this.addButton({id: 'code', group: 'insertions', shortcut: 'Shift+C', action: 'formatCode'});
id: 'code',
group: 'insertions',
shortcut: 'Shift+C',
perform(e) {
if (e.selected.value.indexOf("\n") !== -1) {
e.applySurround(' ', '', 'code_text');
} else {
e.applySurround('`', '`', 'code_text');
}
},
});
this.addButton({ this.addButton({
id: 'bullet', id: 'bullet',
@ -530,6 +519,19 @@ export default Ember.Component.extend({
this.set('insertLinkHidden', false); this.set('insertLinkHidden', false);
}, },
formatCode() {
const sel = this._getSelected();
if (sel.value.indexOf("\n") !== -1) {
return (this.siteSettings.code_formatting_style == "4-spaces-indent") ?
this._applySurround(sel, ' ', '', 'code_text') :
this._addText(sel, '```\n' + sel.value + '\n```');
} else {
return (this.siteSettings.code_formatting_style == "4-spaces-indent") ?
this._applySurround(sel, '`', '`', 'code_text') :
this._applySurround(sel, '```\n', '\n```', 'paste_code_text');
}
},
insertLink() { insertLink() {
const origLink = this.get('linkUrl'); const origLink = this.get('linkUrl');
const linkUrl = (origLink.indexOf('://') === -1) ? `http://${origLink}` : origLink; const linkUrl = (origLink.indexOf('://') === -1) ? `http://${origLink}` : origLink;

View File

@ -1055,6 +1055,7 @@ en:
quote_text: "Blockquote" quote_text: "Blockquote"
code_title: "Preformatted text" code_title: "Preformatted text"
code_text: "indent preformatted text by 4 spaces" code_text: "indent preformatted text by 4 spaces"
paste_code_text: "type or paste code here"
upload_title: "Upload" upload_title: "Upload"
upload_description: "enter upload description here" upload_description: "enter upload description here"
olist_title: "Numbered List" olist_title: "Numbered List"

View File

@ -1315,6 +1315,8 @@ en:
auto_close_messages_post_count: "Maximum number of posts allowed in a message before it is automatically closed (0 to disable)" auto_close_messages_post_count: "Maximum number of posts allowed in a message before it is automatically closed (0 to disable)"
auto_close_topics_post_count: "Maximum number of posts allowed in a topic before it is automatically closed (0 to disable)" auto_close_topics_post_count: "Maximum number of posts allowed in a topic before it is automatically closed (0 to disable)"
code_formatting_style: "Code button in composer will default to this code formatting style"
default_email_digest_frequency: "How often users receive summary emails by default." default_email_digest_frequency: "How often users receive summary emails by default."
default_include_tl0_in_digests: "Include posts from new users in summary emails by default. Users can change this in their preferences." default_include_tl0_in_digests: "Include posts from new users in summary emails by default. Users can change this in their preferences."
default_email_private_messages: "Send an email when someone messages the user by default." default_email_private_messages: "Send an email when someone messages the user by default."

View File

@ -511,6 +511,13 @@ posting:
min: 0 min: 0
auto_close_messages_post_count: 500 auto_close_messages_post_count: 500
auto_close_topics_post_count: 10000 auto_close_topics_post_count: 10000
code_formatting_style:
client: true
type: enum
default: '4-spaces-indent'
choices:
- 4-spaces-indent
- code-fences
email: email:
email_time_window_mins: email_time_window_mins:

View File

@ -257,6 +257,7 @@ testCase('link modal (link with description)', function(assert) {
componentTest('advanced code', { componentTest('advanced code', {
template: '{{d-editor value=value}}', template: '{{d-editor value=value}}',
setup() { setup() {
this.siteSettings.code_formatting_style = '4-spaces-indent';
this.set('value', this.set('value',
`function xyz(x, y, z) { `function xyz(x, y, z) {
if (y === z) { if (y === z) {
@ -286,6 +287,7 @@ componentTest('advanced code', {
componentTest('code button', { componentTest('code button', {
template: '{{d-editor value=value}}', template: '{{d-editor value=value}}',
setup() { setup() {
this.siteSettings.code_formatting_style = '4-spaces-indent';
this.set('value', "first line\n\nsecond line\n\nthird line"); this.set('value', "first line\n\nsecond line\n\nthird line");
}, },