Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const folders = game.folders.contents.filter(f => f.type === "JournalEntry");
- const content = `
- <select>
- ${folders.map(f => `<option value="${f.id}">${f.name}</option>`)}
- </select>
- `;
- const folderId = await Dialog.prompt({
- content,
- title: "Choose a folder",
- rejectClose: false,
- options: {jQuery: false},
- callback: html => html.querySelector("select").value
- });
- if ( !folderId ) return;
- const folder = game.folders.get(folderId);
- const sort = folder.sorting === "m"
- ? SidebarDirectory._sortStandard
- : SidebarDirectory._sortAlphabetical;
- const contents = folder.contents.sort(sort);
- // Maintain a map of journal entries to their converted pages.
- const contentMap = new Map();
- const pages = contents.flatMap((entry, i) => {
- const pages = [];
- // Preserve sort order in the folder.
- let sort = (i + 1) * 200_000;
- const textPage = entry.pages.find(p => p.type === "text")?.toObject();
- const imagePage = entry.pages.find(p => p.type === "image")?.toObject();
- if ( textPage ) {
- textPage._id = foundry.utils.randomID();
- textPage.title.show = true;
- textPage.sort = sort;
- pages.push(textPage);
- sort -= 100_000;
- contentMap.set(entry.id, textPage._id);
- }
- if ( imagePage ) {
- imagePage.sort = sort;
- pages.push(imagePage);
- }
- return pages;
- });
- const entry = await JournalEntry.implementation.create({
- pages,
- name: folder.name,
- folder: folder.folder?.id
- }, {keepEmbeddedIds: true});
- // Update internal links between pages.
- const pageUpdates = [];
- for ( const page of entry.pages.contents.filter(p => p.type === "text") ) {
- let replacements = false;
- const content = page.text.content.replace(/@JournalEntry\[([^\]]+)]/g, (match, originalId) => {
- const targetId = contentMap.get(originalId);
- if ( !targetId ) return match;
- replacements = true;
- const link = entry.pages.get(targetId)._createDocumentLink({}, {relativeTo: page});
- return link.split("{")[0];
- });
- if ( replacements ) pageUpdates.push({_id: page.id, "text.content": content});
- }
- if ( pageUpdates.length ) entry.updateEmbeddedDocuments("JournalEntryPage", pageUpdates);
Add Comment
Please, Sign In to add comment