From bb07b90c3d263625009090327060fb174888b196 Mon Sep 17 00:00:00 2001 From: Dmitry Mazurov Date: Thu, 19 Mar 2020 10:53:40 +0300 Subject: [PATCH] Added DateTimeFormat support for autosave. Signed-off-by: Dmitry Mazurov --- README.md | 13 +++++++++++-- src/js/easymde.js | 38 +++++++++++++++----------------------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 821ca95..bdd327c 100644 --- a/README.md +++ b/README.md @@ -122,7 +122,7 @@ easyMDE.value('New input for **EasyMDE**'); - **delay**: Delay between saves, in milliseconds. Defaults to `10000` (10s). - **submit_delay**: Delay before assuming that submit of the form failed and saving the text, in milliseconds. Defaults to `autosave.delay` or `10000` (10s). - **uniqueId**: You must set a unique string identifier so that EasyMDE can autosave. Something that separates this from other instances of EasyMDE elsewhere on your website. - - **timeFormat**: Time format 12/24. Defaults to 12. + - **timeFormat**: Set DateTimeFormat. More information see [DateTimeFormat instances](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat). - **blockStyles**: Customize how certain buttons that style blocks of text behave. - **bold**: Can be set to `**` or `__`. Defaults to `**`. - **code**: Can be set to ```` ``` ```` or `~~~`. Defaults to ```` ``` ````. @@ -208,7 +208,16 @@ var editor = new EasyMDE({ uniqueId: "MyUniqueID", delay: 1000, submit_delay: 5000, - timeFormat: 24, + timeFormat: { + locale: 'en-US', + format: { + year: 'numeric', + month: 'long', + day: '2-digit', + hour: '2-digit', + minute: '2-digit', + }, + }, }, blockStyles: { bold: "__", diff --git a/src/js/easymde.js b/src/js/easymde.js index c6d0795..49411ef 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -1485,6 +1485,14 @@ var statusTexts = { autosave: 'Autosaved: ', }; +var timeFormat = { + locale: 'en-US', + format: { + hour: '2-digit', + minute: '2-digit', + }, +}; + var blockStyles = { 'bold': '**', 'code': '```', @@ -1620,10 +1628,15 @@ function EasyMDE(options) { // Merging the promptTexts, with the given options options.promptTexts = extend({}, promptTexts, options.promptTexts || {}); + // Merging the statusTexts, with the given options options.statusTexts = extend({}, statusTexts, options.statusTexts || {}); + // Merging the Autosave timeFormat, with the given options + options.autosave.timeFormat = extend({}, timeFormat, options.autosave.timeFormat || {}); + + // Merging the blockStyles, with the given options options.blockStyles = extend({}, blockStyles, options.blockStyles || {}); @@ -2009,31 +2022,10 @@ EasyMDE.prototype.autosave = function () { var el = document.getElementById('autosaved'); if (el != null && el != undefined && el != '') { var d = new Date(); - var hh = d.getHours(); - var m = d.getMinutes(); - var dd = 'am'; - var h = hh; - var html = ''; + var dd = new Intl.DateTimeFormat([this.options.autosave.timeFormat.locale, 'en-US'], this.options.autosave.timeFormat.format).format(d); var save = this.options.statusTexts.autosave; - - m = m < 10 ? '0' + m : m; - if (this.options.autosave.timeFormat == undefined || this.options.autosave.timeFormat == 12) { - if (h >= 12) { - h = hh - 12; - dd = 'pm'; - } - if (h == 0) { - h = 12; - } - - html = save + h + ':' + m + ' ' + dd; - } - if (this.options.autosave.timeFormat == 24) { - html = save + h + ':' + m; - } - - el.innerHTML = html; + el.innerHTML = save + dd; } this.autosaveTimeoutId = setTimeout(function () {