obsidian#requireApiVersion TypeScript Examples

The following examples show how to use obsidian#requireApiVersion. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: util.ts    From obsidian-admonition with MIT License 7 votes vote down vote up
isLivePreview = (state: EditorState) => {
    if (requireApiVersion && requireApiVersion("0.13.23")) {
        return state.field(editorLivePreviewField);
    } else {
        const md = state.field(editorViewField);
        const { state: viewState } = md.leaf.getViewState() ?? {};

        return (
            viewState && viewState.mode == "source" && viewState.source == false
        );
    }
}
Example #2
Source File: baseTypes.ts    From remotely-save with Apache License 2.0 5 votes vote down vote up
VALID_REQURL =
  (!Platform.isAndroidApp && requireApiVersion(API_VER_REQURL)) ||
  (Platform.isAndroidApp && requireApiVersion(API_VER_REQURL_ANDROID))
Example #3
Source File: localdb.ts    From remotely-save with Apache License 2.0 5 votes vote down vote up
insertRenameRecordByVault = async (
  db: InternalDBs,
  fileOrFolder: TAbstractFile,
  oldPath: string,
  vaultRandomID: string
) => {
  // log.info(fileOrFolder);
  let k1: FileFolderHistoryRecord;
  let k2: FileFolderHistoryRecord;
  const actionWhen = Date.now();
  if (fileOrFolder instanceof TFile) {
    k1 = {
      key: oldPath,
      ctime: fileOrFolder.stat.ctime,
      mtime: fileOrFolder.stat.mtime,
      size: fileOrFolder.stat.size,
      actionWhen: actionWhen,
      actionType: "rename",
      keyType: "file",
      renameTo: fileOrFolder.path,
      vaultRandomID: vaultRandomID,
    };
    k2 = {
      key: fileOrFolder.path,
      ctime: fileOrFolder.stat.ctime,
      mtime: fileOrFolder.stat.mtime,
      size: fileOrFolder.stat.size,
      actionWhen: actionWhen,
      actionType: "renameDestination",
      keyType: "file",
      renameTo: "", // itself is the destination, so no need to set this field
      vaultRandomID: vaultRandomID,
    };
  } else if (fileOrFolder instanceof TFolder) {
    const key = oldPath.endsWith("/") ? oldPath : `${oldPath}/`;
    const renameTo = fileOrFolder.path.endsWith("/")
      ? fileOrFolder.path
      : `${fileOrFolder.path}/`;
    let ctime = 0;
    let mtime = 0;
    if (requireApiVersion(API_VER_STAT_FOLDER)) {
      // TAbstractFile does not contain these info
      // but from API_VER_STAT_FOLDER we can manually stat them by path.
      const s = await statFix(fileOrFolder.vault, fileOrFolder.path);
      ctime = s.ctime;
      mtime = s.mtime;
    }
    k1 = {
      key: key,
      ctime: ctime,
      mtime: mtime,
      size: 0,
      actionWhen: actionWhen,
      actionType: "rename",
      keyType: "folder",
      renameTo: renameTo,
      vaultRandomID: vaultRandomID,
    };
    k2 = {
      key: renameTo,
      ctime: ctime,
      mtime: mtime,
      size: 0,
      actionWhen: actionWhen,
      actionType: "renameDestination",
      keyType: "folder",
      renameTo: "", // itself is the destination, so no need to set this field
      vaultRandomID: vaultRandomID,
    };
  }
  await Promise.all([
    db.fileHistoryTbl.setItem(`${vaultRandomID}\t${k1.key}`, k1),
    db.fileHistoryTbl.setItem(`${vaultRandomID}\t${k2.key}`, k2),
  ]);
}
Example #4
Source File: sync.ts    From remotely-save with Apache License 2.0 4 votes vote down vote up
assignOperationToFolderInplace = async (
  origRecord: FileOrFolderMixedState,
  keptFolder: Set<string>,
  vault: Vault,
  password: string = ""
) => {
  let r = origRecord;

  // files and folders are treated differently
  // here we only check folders
  if (!r.key.endsWith("/")) {
    return r;
  }

  if (!keptFolder.has(r.key)) {
    // the folder does NOT have any must-be-kept children!

    if (r.deltimeLocal !== undefined || r.deltimeRemote !== undefined) {
      // it has some deletion "commands"

      const deltimeLocal = r.deltimeLocal !== undefined ? r.deltimeLocal : -1;
      const deltimeRemote =
        r.deltimeRemote !== undefined ? r.deltimeRemote : -1;

      // if it was created after deletion, we should keep it as is
      if (requireApiVersion(API_VER_STAT_FOLDER)) {
        if (r.existLocal) {
          const { ctime, mtime } = await statFix(vault, r.key);
          const cmtime = Math.max(ctime ?? 0, mtime ?? 0);
          if (
            !Number.isNaN(cmtime) &&
            cmtime > 0 &&
            cmtime >= deltimeLocal &&
            cmtime >= deltimeRemote
          ) {
            keptFolder.add(getParentFolder(r.key));
            if (r.existLocal && r.existRemote) {
              r.decision = "skipFolder";
              r.decisionBranch = 14;
            } else if (r.existLocal || r.existRemote) {
              r.decision = "createFolder";
              r.decisionBranch = 15;
            } else {
              throw Error(
                `Error: Folder ${r.key} doesn't exist locally and remotely but is marked must be kept. Abort.`
              );
            }
          }
        }
      }

      // If it was moved to here, after deletion, we should keep it as is.
      // The logic not necessarily needs API_VER_STAT_FOLDER.
      // The folder needs this logic because it's also determined by file children.
      // But the file do not need this logic because the mtimeLocal is checked firstly.
      if (
        r.existLocal &&
        r.changeLocalMtimeUsingMapping &&
        r.mtimeLocal > 0 &&
        r.mtimeLocal > deltimeLocal &&
        r.mtimeLocal > deltimeRemote
      ) {
        keptFolder.add(getParentFolder(r.key));
        if (r.existLocal && r.existRemote) {
          r.decision = "skipFolder";
          r.decisionBranch = 16;
        } else if (r.existLocal || r.existRemote) {
          r.decision = "createFolder";
          r.decisionBranch = 17;
        } else {
          throw Error(
            `Error: Folder ${r.key} doesn't exist locally and remotely but is marked must be kept. Abort.`
          );
        }
      }

      if (r.decision === undefined) {
        // not yet decided by the above reason
        if (deltimeLocal > 0 && deltimeLocal > deltimeRemote) {
          r.decision = "uploadLocalDelHistToRemoteFolder";
          r.decisionBranch = 8;
        } else {
          r.decision = "keepRemoteDelHistFolder";
          r.decisionBranch = 9;
        }
      }
    } else {
      // it does not have any deletion commands
      // keep it as is, and create it if necessary
      keptFolder.add(getParentFolder(r.key));
      if (r.existLocal && r.existRemote) {
        r.decision = "skipFolder";
        r.decisionBranch = 10;
      } else if (r.existLocal || r.existRemote) {
        r.decision = "createFolder";
        r.decisionBranch = 11;
      } else {
        throw Error(
          `Error: Folder ${r.key} doesn't exist locally and remotely but is marked must be kept. Abort.`
        );
      }
    }
  } else {
    // the folder has some must be kept children!
    // so itself and its parent folder must be kept
    keptFolder.add(getParentFolder(r.key));
    if (r.existLocal && r.existRemote) {
      r.decision = "skipFolder";
      r.decisionBranch = 12;
    } else if (r.existLocal || r.existRemote) {
      r.decision = "createFolder";
      r.decisionBranch = 13;
    } else {
      throw Error(
        `Error: Folder ${r.key} doesn't exist locally and remotely but is marked must be kept. Abort.`
      );
    }
  }

  // save the memory, save the world!
  // we have dealt with it, so we don't need it any more.
  keptFolder.delete(r.key);
  return r;
}