plugin.js 6.2 KB

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