plugin.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**
  2. * Copyright (c) Tiny Technologies, Inc. All rights reserved.
  3. * Licensed under the LGPL or a commercial license.
  4. * For LGPL see License.txt in the project root for license information.
  5. * For commercial licenses see https://www.tiny.cloud/
  6. *
  7. * Version: 5.2.0 (2020-02-13)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global = tinymce.util.Tools.resolve('tinymce.PluginManager');
  12. var getDateFormat = function (editor) {
  13. return editor.getParam('insertdatetime_dateformat', editor.translate('%Y-%m-%d'));
  14. };
  15. var getTimeFormat = function (editor) {
  16. return editor.getParam('insertdatetime_timeformat', editor.translate('%H:%M:%S'));
  17. };
  18. var getFormats = function (editor) {
  19. return editor.getParam('insertdatetime_formats', [
  20. '%H:%M:%S',
  21. '%Y-%m-%d',
  22. '%I:%M:%S %p',
  23. '%D'
  24. ]);
  25. };
  26. var getDefaultDateTime = function (editor) {
  27. var formats = getFormats(editor);
  28. return formats.length > 0 ? formats[0] : getTimeFormat(editor);
  29. };
  30. var shouldInsertTimeElement = function (editor) {
  31. return editor.getParam('insertdatetime_element', false);
  32. };
  33. var Settings = {
  34. getDateFormat: getDateFormat,
  35. getTimeFormat: getTimeFormat,
  36. getFormats: getFormats,
  37. getDefaultDateTime: getDefaultDateTime,
  38. shouldInsertTimeElement: shouldInsertTimeElement
  39. };
  40. var daysShort = 'Sun Mon Tue Wed Thu Fri Sat Sun'.split(' ');
  41. var daysLong = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split(' ');
  42. var monthsShort = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
  43. var monthsLong = 'January February March April May June July August September October November December'.split(' ');
  44. var addZeros = function (value, len) {
  45. value = '' + value;
  46. if (value.length < len) {
  47. for (var i = 0; i < len - value.length; i++) {
  48. value = '0' + value;
  49. }
  50. }
  51. return value;
  52. };
  53. var getDateTime = function (editor, fmt, date) {
  54. date = date || new Date();
  55. fmt = fmt.replace('%D', '%m/%d/%Y');
  56. fmt = fmt.replace('%r', '%I:%M:%S %p');
  57. fmt = fmt.replace('%Y', '' + date.getFullYear());
  58. fmt = fmt.replace('%y', '' + date.getYear());
  59. fmt = fmt.replace('%m', addZeros(date.getMonth() + 1, 2));
  60. fmt = fmt.replace('%d', addZeros(date.getDate(), 2));
  61. fmt = fmt.replace('%H', '' + addZeros(date.getHours(), 2));
  62. fmt = fmt.replace('%M', '' + addZeros(date.getMinutes(), 2));
  63. fmt = fmt.replace('%S', '' + addZeros(date.getSeconds(), 2));
  64. fmt = fmt.replace('%I', '' + ((date.getHours() + 11) % 12 + 1));
  65. fmt = fmt.replace('%p', '' + (date.getHours() < 12 ? 'AM' : 'PM'));
  66. fmt = fmt.replace('%B', '' + editor.translate(monthsLong[date.getMonth()]));
  67. fmt = fmt.replace('%b', '' + editor.translate(monthsShort[date.getMonth()]));
  68. fmt = fmt.replace('%A', '' + editor.translate(daysLong[date.getDay()]));
  69. fmt = fmt.replace('%a', '' + editor.translate(daysShort[date.getDay()]));
  70. fmt = fmt.replace('%%', '%');
  71. return fmt;
  72. };
  73. var updateElement = function (editor, timeElm, computerTime, userTime) {
  74. var newTimeElm = editor.dom.create('time', { datetime: computerTime }, userTime);
  75. timeElm.parentNode.insertBefore(newTimeElm, timeElm);
  76. editor.dom.remove(timeElm);
  77. editor.selection.select(newTimeElm, true);
  78. editor.selection.collapse(false);
  79. };
  80. var insertDateTime = function (editor, format) {
  81. if (Settings.shouldInsertTimeElement(editor)) {
  82. var userTime = getDateTime(editor, format);
  83. var computerTime = void 0;
  84. if (/%[HMSIp]/.test(format)) {
  85. computerTime = getDateTime(editor, '%Y-%m-%dT%H:%M');
  86. } else {
  87. computerTime = getDateTime(editor, '%Y-%m-%d');
  88. }
  89. var timeElm = editor.dom.getParent(editor.selection.getStart(), 'time');
  90. if (timeElm) {
  91. updateElement(editor, timeElm, computerTime, userTime);
  92. } else {
  93. editor.insertContent('<time datetime="' + computerTime + '">' + userTime + '</time>');
  94. }
  95. } else {
  96. editor.insertContent(getDateTime(editor, format));
  97. }
  98. };
  99. var Actions = {
  100. insertDateTime: insertDateTime,
  101. getDateTime: getDateTime
  102. };
  103. var register = function (editor) {
  104. editor.addCommand('mceInsertDate', function () {
  105. Actions.insertDateTime(editor, Settings.getDateFormat(editor));
  106. });
  107. editor.addCommand('mceInsertTime', function () {
  108. Actions.insertDateTime(editor, Settings.getTimeFormat(editor));
  109. });
  110. };
  111. var Commands = { register: register };
  112. var global$1 = tinymce.util.Tools.resolve('tinymce.util.Tools');
  113. var Cell = function (initial) {
  114. var value = initial;
  115. var get = function () {
  116. return value;
  117. };
  118. var set = function (v) {
  119. value = v;
  120. };
  121. var clone = function () {
  122. return Cell(get());
  123. };
  124. return {
  125. get: get,
  126. set: set,
  127. clone: clone
  128. };
  129. };
  130. var register$1 = function (editor) {
  131. var formats = Settings.getFormats(editor);
  132. var defaultFormat = Cell(Settings.getDefaultDateTime(editor));
  133. editor.ui.registry.addSplitButton('insertdatetime', {
  134. icon: 'insert-time',
  135. tooltip: 'Insert date/time',
  136. select: function (value) {
  137. return value === defaultFormat.get();
  138. },
  139. fetch: function (done) {
  140. done(global$1.map(formats, function (format) {
  141. return {
  142. type: 'choiceitem',
  143. text: Actions.getDateTime(editor, format),
  144. value: format
  145. };
  146. }));
  147. },
  148. onAction: function () {
  149. var args = [];
  150. for (var _i = 0; _i < arguments.length; _i++) {
  151. args[_i] = arguments[_i];
  152. }
  153. Actions.insertDateTime(editor, defaultFormat.get());
  154. },
  155. onItemAction: function (_, value) {
  156. defaultFormat.set(value);
  157. Actions.insertDateTime(editor, value);
  158. }
  159. });
  160. var makeMenuItemHandler = function (format) {
  161. return function () {
  162. defaultFormat.set(format);
  163. Actions.insertDateTime(editor, format);
  164. };
  165. };
  166. editor.ui.registry.addNestedMenuItem('insertdatetime', {
  167. icon: 'insert-time',
  168. text: 'Date/time',
  169. getSubmenuItems: function () {
  170. return global$1.map(formats, function (format) {
  171. return {
  172. type: 'menuitem',
  173. text: Actions.getDateTime(editor, format),
  174. onAction: makeMenuItemHandler(format)
  175. };
  176. });
  177. }
  178. });
  179. };
  180. var Buttons = { register: register$1 };
  181. function Plugin () {
  182. global.add('insertdatetime', function (editor) {
  183. Commands.register(editor);
  184. Buttons.register(editor);
  185. });
  186. }
  187. Plugin();
  188. }());