sidebarpreviewtext.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-License-Identifier: AGPL-3.0-or-later
  4. */
  5. (function () {
  6. var SidebarPreview = function () {
  7. };
  8. SidebarPreview.prototype = {
  9. attach: function (manager) {
  10. manager.addPreviewHandler('text', this.handlePreview.bind(this));
  11. },
  12. handlePreview: function (model, $thumbnailDiv, $thumbnailContainer, fallback) {
  13. var previewWidth = $thumbnailContainer.parent().width() + 50; // 50px for negative margins
  14. var previewHeight = previewWidth / (16 / 9);
  15. this.getFileContent(model.getFullPath()).then(function (content) {
  16. $thumbnailDiv.removeClass('icon-loading icon-32');
  17. $thumbnailContainer.addClass('large');
  18. $thumbnailContainer.addClass('text');
  19. var $textPreview = $('<pre></pre>').text(content);
  20. $thumbnailDiv.children('.stretcher').remove();
  21. $thumbnailDiv.append($textPreview);
  22. $thumbnailContainer.css("max-height", previewHeight);
  23. }, function () {
  24. fallback();
  25. });
  26. },
  27. getFileContent: function (path) {
  28. return $.ajax({
  29. url: OC.linkToRemoteBase('files' + path),
  30. headers: {
  31. 'Range': 'bytes=0-10240'
  32. }
  33. });
  34. }
  35. };
  36. OC.Plugins.register('OCA.Files.SidebarPreviewManager', new SidebarPreview());
  37. })();