From 83309752ae0fc224fadc73cd410dc682e7e54b3c Mon Sep 17 00:00:00 2001
From: Arpit Jalan <arpit@techapj.com>
Date: Mon, 27 Jun 2016 00:00:45 +0530
Subject: [PATCH] FEATURE: new site setting 'code formatting style'

---
 .../discourse/components/d-editor.js.es6      | 26 ++++++++++---------
 config/locales/client.en.yml                  |  1 +
 config/locales/server.en.yml                  |  2 ++
 config/site_settings.yml                      |  7 +++++
 .../components/d-editor-test.js.es6           |  2 ++
 5 files changed, 26 insertions(+), 12 deletions(-)

diff --git a/app/assets/javascripts/discourse/components/d-editor.js.es6 b/app/assets/javascripts/discourse/components/d-editor.js.es6
index 6ff564d54f4..90ef37e0c7e 100644
--- a/app/assets/javascripts/discourse/components/d-editor.js.es6
+++ b/app/assets/javascripts/discourse/components/d-editor.js.es6
@@ -63,18 +63,7 @@ class Toolbar {
       perform: e => e.applySurround('> ', '', 'code_text')
     });
 
-    this.addButton({
-      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({id: 'code', group: 'insertions', shortcut: 'Shift+C', action: 'formatCode'});
 
     this.addButton({
       id: 'bullet',
@@ -530,6 +519,19 @@ export default Ember.Component.extend({
       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() {
       const origLink = this.get('linkUrl');
       const linkUrl = (origLink.indexOf('://') === -1) ? `http://${origLink}` : origLink;
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 3d142ef2c0e..0f83190a42e 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -1055,6 +1055,7 @@ en:
       quote_text: "Blockquote"
       code_title: "Preformatted text"
       code_text: "indent preformatted text by 4 spaces"
+      paste_code_text: "type or paste code here"
       upload_title: "Upload"
       upload_description: "enter upload description here"
       olist_title: "Numbered List"
diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml
index 45cd768c1d5..bb07b0708b9 100644
--- a/config/locales/server.en.yml
+++ b/config/locales/server.en.yml
@@ -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_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_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."
diff --git a/config/site_settings.yml b/config/site_settings.yml
index d3b255bcf30..bbc3bad8e12 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -511,6 +511,13 @@ posting:
     min: 0
   auto_close_messages_post_count: 500
   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_time_window_mins:
diff --git a/test/javascripts/components/d-editor-test.js.es6 b/test/javascripts/components/d-editor-test.js.es6
index 23252b214d9..16bbbd063d1 100644
--- a/test/javascripts/components/d-editor-test.js.es6
+++ b/test/javascripts/components/d-editor-test.js.es6
@@ -257,6 +257,7 @@ testCase('link modal (link with description)', function(assert) {
 componentTest('advanced code', {
   template: '{{d-editor value=value}}',
   setup() {
+    this.siteSettings.code_formatting_style = '4-spaces-indent';
     this.set('value',
 `function xyz(x, y, z) {
   if (y === z) {
@@ -286,6 +287,7 @@ componentTest('advanced code', {
 componentTest('code button', {
   template: '{{d-editor value=value}}',
   setup() {
+    this.siteSettings.code_formatting_style = '4-spaces-indent';
     this.set('value', "first line\n\nsecond line\n\nthird line");
   },