plugin.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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.10.2 (2021-11-17)
  8. */
  9. (function () {
  10. 'use strict';
  11. var global$1 = 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 daysShort = 'Sun Mon Tue Wed Thu Fri Sat Sun'.split(' ');
  34. var daysLong = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sunday'.split(' ');
  35. var monthsShort = 'Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec'.split(' ');
  36. var monthsLong = 'January February March April May June July August September October November December'.split(' ');
  37. var addZeros = function (value, len) {
  38. value = '' + value;
  39. if (value.length < len) {
  40. for (var i = 0; i < len - value.length; i++) {
  41. value = '0' + value;
  42. }
  43. }
  44. return value;
  45. };
  46. var getDateTime = function (editor, fmt, date) {
  47. if (date === void 0) {
  48. date = new Date();
  49. }
  50. fmt = fmt.replace('%D', '%m/%d/%Y');
  51. fmt = fmt.replace('%r', '%I:%M:%S %p');
  52. fmt = fmt.replace('%Y', '' + date.getFullYear());
  53. fmt = fmt.replace('%y', '' + date.getYear());
  54. fmt = fmt.replace('%m', addZeros(date.getMonth() + 1, 2));
  55. fmt = fmt.replace('%d', addZeros(date.getDate(), 2));
  56. fmt = fmt.replace('%H', '' + addZeros(date.getHours(), 2));
  57. fmt = fmt.replace('%M', '' + addZeros(date.getMinutes(), 2));
  58. fmt = fmt.replace('%S', '' + addZeros(date.getSeconds(), 2));
  59. fmt = fmt.replace('%I', '' + ((date.getHours() + 11) % 12 + 1));
  60. fmt = fmt.replace('%p', '' + (date.getHours() < 12 ? 'AM' : 'PM'));
  61. fmt = fmt.replace('%B', '' + editor.translate(monthsLong[date.getMonth()]));
  62. fmt = fmt.replace('%b', '' + editor.translate(monthsShort[date.getMonth()]));
  63. fmt = fmt.replace('%A', '' + editor.translate(daysLong[date.getDay()]));
  64. fmt = fmt.replace('%a', '' + editor.translate(daysShort[date.getDay()]));
  65. fmt = fmt.replace('%%', '%');
  66. return fmt;
  67. };
  68. var updateElement = function (editor, timeElm, computerTime, userTime) {
  69. var newTimeElm = editor.dom.create('time', { datetime: computerTime }, userTime);
  70. timeElm.parentNode.insertBefore(newTimeElm, timeElm);
  71. editor.dom.remove(timeElm);
  72. editor.selection.select(newTimeElm, true);
  73. editor.selection.collapse(false);
  74. };
  75. var insertDateTime = function (editor, format) {
  76. if (shouldInsertTimeElement(editor)) {
  77. var userTime = getDateTime(editor, format);
  78. var computerTime = void 0;
  79. if (/%[HMSIp]/.test(format)) {
  80. computerTime = getDateTime(editor, '%Y-%m-%dT%H:%M');
  81. } else {
  82. computerTime = getDateTime(editor, '%Y-%m-%d');
  83. }
  84. var timeElm = editor.dom.getParent(editor.selection.getStart(), 'time');
  85. if (timeElm) {
  86. updateElement(editor, timeElm, computerTime, userTime);
  87. } else {
  88. editor.insertContent('<time datetime="' + computerTime + '">' + userTime + '</time>');
  89. }
  90. } else {
  91. editor.insertContent(getDateTime(editor, format));
  92. }
  93. };
  94. var register$1 = function (editor) {
  95. editor.addCommand('mceInsertDate', function (_ui, value) {
  96. insertDateTime(editor, value !== null && value !== void 0 ? value : getDateFormat(editor));
  97. });
  98. editor.addCommand('mceInsertTime', function (_ui, value) {
  99. insertDateTime(editor, value !== null && value !== void 0 ? value : getTimeFormat(editor));
  100. });
  101. };
  102. var Cell = function (initial) {
  103. var value = initial;
  104. var get = function () {
  105. return value;
  106. };
  107. var set = function (v) {
  108. value = v;
  109. };
  110. return {
  111. get: get,
  112. set: set
  113. };
  114. };
  115. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  116. var register = function (editor) {
  117. var formats = getFormats(editor);
  118. var defaultFormat = Cell(getDefaultDateTime(editor));
  119. var insertDateTime = function (format) {
  120. return editor.execCommand('mceInsertDate', false, format);
  121. };
  122. editor.ui.registry.addSplitButton('insertdatetime', {
  123. icon: 'insert-time',
  124. tooltip: 'Insert date/time',
  125. select: function (value) {
  126. return value === defaultFormat.get();
  127. },
  128. fetch: function (done) {
  129. done(global.map(formats, function (format) {
  130. return {
  131. type: 'choiceitem',
  132. text: getDateTime(editor, format),
  133. value: format
  134. };
  135. }));
  136. },
  137. onAction: function (_api) {
  138. insertDateTime(defaultFormat.get());
  139. },
  140. onItemAction: function (_api, value) {
  141. defaultFormat.set(value);
  142. insertDateTime(value);
  143. }
  144. });
  145. var makeMenuItemHandler = function (format) {
  146. return function () {
  147. defaultFormat.set(format);
  148. insertDateTime(format);
  149. };
  150. };
  151. editor.ui.registry.addNestedMenuItem('insertdatetime', {
  152. icon: 'insert-time',
  153. text: 'Date/time',
  154. getSubmenuItems: function () {
  155. return global.map(formats, function (format) {
  156. return {
  157. type: 'menuitem',
  158. text: getDateTime(editor, format),
  159. onAction: makeMenuItemHandler(format)
  160. };
  161. });
  162. }
  163. });
  164. };
  165. function Plugin () {
  166. global$1.add('insertdatetime', function (editor) {
  167. register$1(editor);
  168. register(editor);
  169. });
  170. }
  171. Plugin();
  172. }());