plugin.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * TinyMCE version 6.4.2 (2023-04-26)
  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. editor.dom.replace(newTimeElm, timeElm);
  75. editor.selection.select(newTimeElm, true);
  76. editor.selection.collapse(false);
  77. };
  78. const insertDateTime = (editor, format) => {
  79. if (shouldInsertTimeElement(editor)) {
  80. const userTime = getDateTime(editor, format);
  81. let computerTime;
  82. if (/%[HMSIp]/.test(format)) {
  83. computerTime = getDateTime(editor, '%Y-%m-%dT%H:%M');
  84. } else {
  85. computerTime = getDateTime(editor, '%Y-%m-%d');
  86. }
  87. const timeElm = editor.dom.getParent(editor.selection.getStart(), 'time');
  88. if (timeElm) {
  89. updateElement(editor, timeElm, computerTime, userTime);
  90. } else {
  91. editor.insertContent('<time datetime="' + computerTime + '">' + userTime + '</time>');
  92. }
  93. } else {
  94. editor.insertContent(getDateTime(editor, format));
  95. }
  96. };
  97. const register$1 = editor => {
  98. editor.addCommand('mceInsertDate', (_ui, value) => {
  99. insertDateTime(editor, value !== null && value !== void 0 ? value : getDateFormat(editor));
  100. });
  101. editor.addCommand('mceInsertTime', (_ui, value) => {
  102. insertDateTime(editor, value !== null && value !== void 0 ? value : getTimeFormat(editor));
  103. });
  104. };
  105. const Cell = initial => {
  106. let value = initial;
  107. const get = () => {
  108. return value;
  109. };
  110. const set = v => {
  111. value = v;
  112. };
  113. return {
  114. get,
  115. set
  116. };
  117. };
  118. var global = tinymce.util.Tools.resolve('tinymce.util.Tools');
  119. const register = editor => {
  120. const formats = getFormats(editor);
  121. const defaultFormat = Cell(getDefaultDateTime(editor));
  122. const insertDateTime = format => editor.execCommand('mceInsertDate', false, format);
  123. editor.ui.registry.addSplitButton('insertdatetime', {
  124. icon: 'insert-time',
  125. tooltip: 'Insert date/time',
  126. select: value => value === defaultFormat.get(),
  127. fetch: done => {
  128. done(global.map(formats, format => ({
  129. type: 'choiceitem',
  130. text: getDateTime(editor, format),
  131. value: format
  132. })));
  133. },
  134. onAction: _api => {
  135. insertDateTime(defaultFormat.get());
  136. },
  137. onItemAction: (_api, value) => {
  138. defaultFormat.set(value);
  139. insertDateTime(value);
  140. }
  141. });
  142. const makeMenuItemHandler = format => () => {
  143. defaultFormat.set(format);
  144. insertDateTime(format);
  145. };
  146. editor.ui.registry.addNestedMenuItem('insertdatetime', {
  147. icon: 'insert-time',
  148. text: 'Date/time',
  149. getSubmenuItems: () => global.map(formats, format => ({
  150. type: 'menuitem',
  151. text: getDateTime(editor, format),
  152. onAction: makeMenuItemHandler(format)
  153. }))
  154. });
  155. };
  156. var Plugin = () => {
  157. global$1.add('insertdatetime', editor => {
  158. register$2(editor);
  159. register$1(editor);
  160. register(editor);
  161. });
  162. };
  163. Plugin();
  164. })();