Guest User

Macro pour consolider des journaux Foundry v9 en un journal v10

a guest
Apr 14th, 2023
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const folders = game.folders.contents.filter(f => f.type === "JournalEntry");
  2. const content = `
  3.   <select>
  4.     ${folders.map(f => `<option value="${f.id}">${f.name}</option>`)}
  5.   </select>
  6. `;
  7. const folderId = await Dialog.prompt({
  8.   content,
  9.   title: "Choose a folder",
  10.   rejectClose: false,
  11.   options: {jQuery: false},
  12.   callback: html => html.querySelector("select").value
  13. });
  14. if ( !folderId ) return;
  15. const folder = game.folders.get(folderId);
  16. const sort = folder.sorting === "m"
  17.   ? SidebarDirectory._sortStandard
  18.   : SidebarDirectory._sortAlphabetical;
  19. const contents = folder.contents.sort(sort);
  20. // Maintain a map of journal entries to their converted pages.
  21. const contentMap = new Map();
  22. const pages = contents.flatMap((entry, i) => {
  23.   const pages = [];
  24.   // Preserve sort order in the folder.
  25.   let sort = (i + 1) * 200_000;
  26.   const textPage = entry.pages.find(p => p.type === "text")?.toObject();
  27.   const imagePage = entry.pages.find(p => p.type === "image")?.toObject();
  28.   if ( textPage ) {
  29.     textPage._id = foundry.utils.randomID();
  30.     textPage.title.show = true;
  31.     textPage.sort = sort;
  32.     pages.push(textPage);
  33.     sort -= 100_000;
  34.     contentMap.set(entry.id, textPage._id);
  35.   }
  36.   if ( imagePage ) {
  37.     imagePage.sort = sort;
  38.     pages.push(imagePage);
  39.   }
  40.   return pages;
  41. });
  42. const entry = await JournalEntry.implementation.create({
  43.   pages,
  44.   name: folder.name,
  45.   folder: folder.folder?.id
  46. }, {keepEmbeddedIds: true});
  47. // Update internal links between pages.
  48. const pageUpdates = [];
  49. for ( const page of entry.pages.contents.filter(p => p.type === "text") ) {
  50.   let replacements = false;
  51.   const content = page.text.content.replace(/@JournalEntry\[([^\]]+)]/g, (match, originalId) => {
  52.     const targetId = contentMap.get(originalId);
  53.     if ( !targetId ) return match;
  54.     replacements = true;
  55.     const link = entry.pages.get(targetId)._createDocumentLink({}, {relativeTo: page});
  56.     return link.split("{")[0];
  57.   });
  58.   if ( replacements ) pageUpdates.push({_id: page.id, "text.content": content});
  59. }
  60. if ( pageUpdates.length ) entry.updateEmbeddedDocuments("JournalEntryPage", pageUpdates);
Add Comment
Please, Sign In to add comment