lib/editor/tiny/amd/src/content.js

  1. // This file is part of Moodle - http://moodle.org/
  2. //
  3. // Moodle is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation, either version 3 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // Moodle is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License
  14. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  15. /**
  16. * Module to assist with creation and management of content.
  17. *
  18. * @module editor_tiny/content
  19. * @copyright Andrew Lyons <andrew@nicols.co.uk>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. /**
  23. * Add MathML support to the editor.
  24. *
  25. * @param {TinyMCE} editor
  26. */
  27. export const addMathMLSupport = (editor) => {
  28. const getNodeType = (node) => {
  29. const style = node.attr('style');
  30. if (style?.includes('display')) {
  31. if (style.match(/display:[^;]*inline/)) {
  32. return 'tiny-math-span';
  33. }
  34. }
  35. return 'tiny-math-block';
  36. };
  37. editor.on('PreInit', () => {
  38. editor.schema.addCustomElements({
  39. // Add support for MathML by defining some tiny-math blocks which extends SPAN/DIV.
  40. // Note: This is blind support and does not check the child content.
  41. // Any invalid markup will be accepted.
  42. // Note: We use the same names as the Tiny Premium Math plugin to avoid conflicts if both are enabled.
  43. math: {
  44. 'extends': 'div',
  45. },
  46. 'tiny-math-span': {
  47. 'extends': "span",
  48. },
  49. 'tiny-math-block': {
  50. 'extends': "div",
  51. },
  52. });
  53. // Add a Parser filter to wrap math nodes in a tiny-math-[block|span] element.
  54. editor.parser.addNodeFilter('math', (nodes) => nodes.forEach((node) => {
  55. if (node.parent) {
  56. if (node.parent.name === 'tiny-math-block' || node.parent.name === 'tiny-math-span') {
  57. // Already wrapped.
  58. return;
  59. }
  60. }
  61. const displayMode = getNodeType(node);
  62. node.wrap(editor.editorManager.html.Node.create(displayMode, {
  63. contenteditable: 'false',
  64. }));
  65. }));
  66. // Add a Serializer filter to remove the tiny-math-[block|span] wrapper.
  67. editor.serializer.addNodeFilter('tiny-math-span, tiny-math-block', (nodes, name) => nodes.forEach((node) => {
  68. const displayMode = name.replace('tiny-math-', '');
  69. node.children().forEach((child) => {
  70. const currentStyle = child.attr('style');
  71. if (currentStyle) {
  72. child.attr('style', `${currentStyle};display: ${displayMode}`);
  73. } else {
  74. child.attr('style', `display: ${displayMode}`);
  75. }
  76. });
  77. node.unwrap();
  78. }));
  79. });
  80. };
  81. /**
  82. * Add SVG support to the editor.
  83. *
  84. * @param {TinyMCE} editor
  85. */
  86. export const addSVGSupport = (editor) => {
  87. editor.on('PreInit', () => {
  88. editor.schema.addCustomElements({
  89. // Add support for SVG by defining an SVG tag which extends DIV.
  90. // Note: This is blind support and does not check the child content.
  91. // Any invalid markup will be accepted.
  92. svg: {
  93. 'extends': "div",
  94. },
  95. 'tiny-svg-block': {
  96. 'extends': "div",
  97. },
  98. });
  99. editor.parser.addNodeFilter('svg', (nodes) => nodes.forEach((node) => {
  100. node.wrap(editor.editorManager.html.Node.create('tiny-svg-block', {
  101. contenteditable: 'false',
  102. }));
  103. }));
  104. editor.serializer.addNodeFilter('tiny-svg-block', (nodes) => nodes.forEach((node) => {
  105. node.unwrap();
  106. }));
  107. });
  108. };