Skip to content

armgit / plane

Public Imported from GitHub
0

πŸ”₯πŸ”₯πŸ”₯ Open-source Jira, Linear, Monday, and ClickUp alternative. Plane is a modern project management platform to manage tasks, sprints, docs, and triage.

1.2 KB
Raw Blame
/**
 * Copyright (c) 2023-present Plane Software, Inc. and contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 * See the LICENSE file for details.
 */

import type { Editor } from "@tiptap/core";
// constants
import { CORE_EXTENSIONS } from "@/constants/extension";
// types
import type { TFileHandler } from "@/types";

/**
 * Finds all public image nodes in the document and restores them using the provided restore function
 *
 * Never remove this onCreate hook, it's a hack to restore old public
 * images, since they don't give error if they've been deleted as they are
 * rendered directly from image source instead of going through the
 * apiserver
 */
export const restorePublicImages = (editor: Editor, restoreImageFn: TFileHandler["restore"]) => {
  const imageSources = new Set<string>();
  editor.state.doc.descendants((node) => {
    if ([CORE_EXTENSIONS.IMAGE, CORE_EXTENSIONS.CUSTOM_IMAGE].includes(node.type.name as CORE_EXTENSIONS)) {
      if (!node.attrs.src?.startsWith("http")) return;

      imageSources.add(node.attrs.src);
    }
  });

  imageSources.forEach(async (src) => {
    try {
      await restoreImageFn(src);
    } catch (error) {
      console.error("Error restoring image: ", error);
    }
  });
};