lib/editor/tiny/plugins/media/amd/src/usedfiles.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. * Tiny Media Manager usedfiles.
  17. *
  18. * @module tiny_media/usedfiles
  19. * @copyright 2022, Stevani Andolo <stevani@hotmail.com.au>
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  21. */
  22. import * as Templates from 'core/templates';
  23. import Config from 'core/config';
  24. class UsedFileManager {
  25. constructor(files, userContext, itemId, elementId) {
  26. this.files = files;
  27. this.userContext = userContext;
  28. this.itemId = itemId;
  29. this.elementId = elementId;
  30. }
  31. getElementId() {
  32. return this.elementId;
  33. }
  34. getUsedFiles() {
  35. const editor = window.parent.tinymce.EditorManager.get(this.getElementId());
  36. if (!editor) {
  37. window.console.error(`Editor not found for ${this.getElementId()}`);
  38. return [];
  39. }
  40. const content = editor.getContent();
  41. const baseUrl = `${Config.wwwroot}/draftfile.php/${this.userContext}/user/draft/${this.itemId}/`;
  42. const pattern = new RegExp("[\"']" + baseUrl.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&') + "(?<filename>.+?)[\\?\"']", 'gm');
  43. const usedFiles = [...content.matchAll(pattern)].map((match) => decodeURIComponent(match.groups.filename));
  44. return usedFiles;
  45. }
  46. // Return an array of unused files.
  47. findUnusedFiles(usedFiles) {
  48. return Object.entries(this.files)
  49. .filter(([filename]) => !usedFiles.includes(filename))
  50. .map(([filename]) => filename);
  51. }
  52. // Return an array of missing files.
  53. findMissingFiles(usedFiles) {
  54. return usedFiles.filter((filename) => !this.files.hasOwnProperty(filename));
  55. }
  56. updateFiles() {
  57. const form = document.querySelector('form');
  58. const usedFiles = this.getUsedFiles();
  59. const unusedFiles = this.findUnusedFiles(usedFiles);
  60. const missingFiles = this.findMissingFiles(usedFiles);
  61. form.querySelectorAll('input[type=checkbox][name^="deletefile"]').forEach((checkbox) => {
  62. if (!unusedFiles.includes(checkbox.dataset.filename)) {
  63. checkbox.closest('.fitem').remove();
  64. }
  65. });
  66. form.classList.toggle('has-missing-files', !!missingFiles.length);
  67. form.classList.toggle('has-unused-files', !!unusedFiles.length);
  68. return Templates.renderForPromise('tiny_media/missingfiles', {
  69. missingFiles,
  70. }).then(({html, js}) => {
  71. Templates.replaceNodeContents(form.querySelector('.missing-files'), html, js);
  72. return;
  73. });
  74. }
  75. }
  76. export const init = (files, usercontext, itemid, elementid) => {
  77. const manager = new UsedFileManager(files, usercontext, itemid, elementid);
  78. manager.updateFiles();
  79. return manager;
  80. };