From 1e64cba17ee406afbdc489fb11a089bbf9ea4ae5 Mon Sep 17 00:00:00 2001 From: Dmitry Mazurov Date: Thu, 19 Mar 2020 12:17:40 +0300 Subject: [PATCH] Added DateTimeFormat support for autosave. Signed-off-by: Dmitry Mazurov --- README.md | 35 ++++++++++++++++++++++------------- src/js/easymde.js | 39 ++++++++++++++++----------------------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 48fb244..ba8a756 100644 --- a/README.md +++ b/README.md @@ -25,18 +25,18 @@ The editor is entirely customizable, from theming to toolbar buttons and javascr - [Install EasyMDE](#install-easymde) - [How to use](#how-to-use) - - [Loading the editor](#loading-the-editor) - - [Editor functions](#editor-functions) + - [Loading the editor](#loading-the-editor) + - [Editor functions](#editor-functions) - [Configuration](#configuration) - - [Options list](#options-list) - - [Options example](#options-example) - - [Toolbar icons](#toolbar-icons) - - [Toolbar customization](#toolbar-customization) - - [Keyboard shortcuts](#keyboard-shortcuts) + - [Options list](#options-list) + - [Options example](#options-example) + - [Toolbar icons](#toolbar-icons) + - [Toolbar customization](#toolbar-customization) + - [Keyboard shortcuts](#keyboard-shortcuts) - [Advanced use](#advanced-use) - - [Event handling](#event-handling) - - [Removing EasyMDE from text area](#removing-easymde-from-text-area) - - [Useful methods](#useful-methods) + - [Event handling](#event-handling) + - [Removing EasyMDE from text area](#removing-easymde-from-text-area) + - [Useful methods](#useful-methods) - [How it works](#how-it-works) - [SimpleMDE fork](#simplemde-fork) - [Hacking EasyMDE](#hacking-easymde) @@ -122,8 +122,8 @@ 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. -- **text**: Set text for autosave. + - **timeFormat**: Set DateTimeFormat. More information see [DateTimeFormat instances](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat). Default `locale: en-US, format: hour:minute`. + - **text**: Set text for autosave. - **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', + }, + }, text: "Autosaved: " }, blockStyles: { diff --git a/src/js/easymde.js b/src/js/easymde.js index 70ec8fa..063cebb 100644 --- a/src/js/easymde.js +++ b/src/js/easymde.js @@ -1479,6 +1479,14 @@ var promptTexts = { image: 'URL of the image:', }; +var timeFormat = { + locale: 'en-US', + format: { + hour: '2-digit', + minute: '2-digit', + }, +}; + var blockStyles = { 'bold': '**', 'code': '```', @@ -1619,6 +1627,12 @@ function EasyMDE(options) { options.blockStyles = extend({}, blockStyles, options.blockStyles || {}); + if (options.autosave != undefined) { + // Merging the Autosave timeFormat, with the given options + options.autosave.timeFormat = extend({}, timeFormat, options.autosave.timeFormat || {}); + } + + // Merging the shortcuts, with the given options options.shortcuts = extend({}, shortcuts, options.shortcuts || {}); @@ -2000,31 +2014,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.autosave.text == undefined ? 'Autosaved: ' : this.options.autosave.text; - 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 () {