Skip to content

Commit

Permalink
sync button; auto sync; example image
Browse files Browse the repository at this point in the history
  • Loading branch information
Sigafoos committed Sep 3, 2020
1 parent f9bc045 commit acd6265
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@ A module for Foundry VTT that links entities (journal entries, actors and items)

This module adds wiki-style "referenced by" links to journals, actors and items, allowing easy browsing. It doesn't change the actual text of the journal/bio/etc, but displays it as though it was part of the entry!

![a variety of entities showing links](example.png)

## Installing
Add the manifest to your Foundry modules: https://raw.githubusercontent.com/Sigafoos/journal-links/master/module.json

(automatic discovery/install in Foundry itself coming soon)

## Linking entities
In general the linking should happen automatically: this module detects that it hasn't ever run and will run an initial sync. Whenever you save an entity (journal entry, actor bio, item description) it will detect all entities linked in the text and link them if the "Rebuild on save" setting is enabled (there shouldn't be a reason to disable it, as I don't anticipate performance issues, but it's included just in case).

If you've disabled "Rebuild on save" (or just feel like it), in the module settings you can click the 'Sync now' button to perform a sync.

The module may perform additional automatic syncs after an update which fixes a bug, etc.

## Settings
* **Rebuild on save**: if disabled, won't automatically generate links between entities (default: enabled)
* **Heading tag**: by default it uses `<h1>` tags for the section. If you'd like to change it to `<h2>`, etc, you can (note: this doesn't support custom classes, attributes, etc)
Expand Down
Binary file added example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion module.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "journal-links",
"title": "Journal Links",
"description": "Include links to entities that reference the viewed journal, actor, item, etc.",
"version": "0.2.0",
"version": "0.3.0",
"author": "dconley",
"esmodules": ["./scripts/index.js"],
"scripts": [],
Expand Down
35 changes: 30 additions & 5 deletions scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { JournalLink } from './journallink.js';
//import { JournalLinkSettings } from './settings.js';
import { Sync } from './sync.js';

// bump this to cause a sync on page load (one time)
const SYNC_VERSION = 1;

const MODULE_NAME = 'journal-links';
const NAME = 'Journal Links';
Expand All @@ -25,18 +28,31 @@ Hooks.on("init", () => {
});
game.settings.register(MODULE_NAME, 'debug', {
name : 'Debug logging',
scope: 'world',
config: 'client',
scope: 'client',
config: true,
type: Boolean,
default: false
});
game.settings.register(MODULE_NAME, 'lastSyncedVersion', {
name : 'Last synced version',
hint: 'If we perform a bugfix that would benefit from resyncing, SYNC_VERSION will be out of -- forgive me -- sync, indicating we should perform a sync',
scope: 'world',
config: false,
type: Number,
default: 0
});
game.settings.registerMenu(MODULE_NAME, 'syncButton', {
name: 'Sync entries',
label: 'Sync now',
icon: 'fas fa-sync-alt',
type: Sync,
restricted: true
});

let jl = new JournalLink();
game.JournalLink = jl;
CONFIG.debug.JournalLinks = game.settings.get(MODULE_NAME, 'debug');

// roll tables have no context of this stuff

// things what update
Hooks.on('updateJournalEntry', game.JournalLink.updateJournalEntry.bind(jl));
Hooks.on('updateActor', game.JournalLink.updateActor.bind(jl));
Expand All @@ -46,4 +62,13 @@ Hooks.on("init", () => {
Hooks.on('renderJournalSheet', game.JournalLink.includeJournalLinks.bind(jl));
Hooks.on('renderActorSheet', game.JournalLink.includeActorLinks.bind(jl));
Hooks.on('renderItemSheet', game.JournalLink.includeItemLinks.bind(jl));

// initial sync
Hooks.on('ready', () => {
if (game.settings.get(MODULE_NAME, 'lastSyncedVersion') < SYNC_VERSION) {
console.log('journal-links | performing sync...');
game.JournalLink.sync();
game.settings.set(MODULE_NAME, 'lastSyncedVersion', SYNC_VERSION);
}
});
});
16 changes: 12 additions & 4 deletions scripts/journallink.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,31 @@ export class JournalLink {
];

updateJournalEntry({ data }) {
if (!game.settings.get('journal-links', 'rebuildOnSave')) {
this.log('not updating ' + entityType + ' ' + data.name + ' as rebuildOnSave is false');
return;
}
this.update(data, 'JournalEntry', data.content);
}

updateActor({ data }) {
if (!game.settings.get('journal-links', 'rebuildOnSave')) {
this.log('not updating ' + entityType + ' ' + data.name + ' as rebuildOnSave is false');
return;
}
this.update(data, 'Actor', data.data.details.biography.value);
}

updateItem({ data }) {
if (!game.settings.get('journal-links', 'rebuildOnSave')) {
this.log('not updating ' + entityType + ' ' + data.name + ' as rebuildOnSave is false');
return;
}
this.update(data, 'Item', data.data.description.value);
}

// TODO is the lack of async/await here going to bite me?
update(data, entityType, content) {
if (!game.settings.get('journal-links', 'rebuildOnSave')) {
this.log('not updating ' + entityType + ' ' + data.name + ' as rebuildOnSave is false');
return;
}
this.log('updating ' + entityType + ' ' + data.name);

let references = this.references(content);
Expand Down
21 changes: 21 additions & 0 deletions scripts/sync.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// this is handled as a setting menu but only uses that as a hack to do its work
export class Sync extends FormApplication {
constructor(object = {}, options) {
super(object, options);

// no idea how this would happen, but CYA
if (!game.JournalLink) {
ui.notifications.warn('JournalLink object not found; cannot sync')
} else {
ui.notifications.info('Syncing journal links');
game.JournalLink.sync();
ui.notifications.info('Journal link sync completed');
}

// there shouldn't be anything to close, but just in case!
this.close();
}

// not overriding this causes it to try to actually render, throwing an error
render = () => {};
}

0 comments on commit acd6265

Please sign in to comment.