diff --git a/scripts/mills_messages.mjs b/scripts/mills_messages.mjs index 15ddffd..341161a 100644 --- a/scripts/mills_messages.mjs +++ b/scripts/mills_messages.mjs @@ -1,58 +1,13 @@ -export const module_id = "the-mills-messages"; - +import { module_id, history_flag, str_format, create_history_journal } from "./utils.mjs" import { module_settings, register_settings } from "./settings.mjs"; import { prepare_send_message_html, validate_form } from "./send_dialog_form.mjs"; -// User flag to point to the ID of the journal to store message history to -const history_flag = "history-journal"; - /** @type {SocketlibSocket} */ let socket; const SocketMessages = Object.freeze({ DisplayMessage: "display-message", }) -/** - * This is a copy of Foundry's internal formatting used by - * localize.format(), so that we can be consistent. - * @param {String} str The string whose "{named_args}" will be formatted - * @param {Object} data An object with named_args members, whose data is replaced - * @returns {String} The formatted string - */ -function str_format(str, data) { - const fmt = /{[^}]+}/g; - str = str.replace(fmt, k => { - return data[k.slice(1, -1)]; - }); - return str; -} - -/** - * Create the journal entry for the history of the specified user, - * update the user's history flag and return the entry for further - * manipulation if needed. - * @param {User} user The user object that we are creating the history for - * @return {Promise} The JournalEntry that was created. - */ -async function create_history_journal(user) { - // Use the settings' title for the journal, or revert to the default one if it is empty. - let format_name = {name: user.character ? user.character.name : user.name}; - let formated_journal_title = str_format(game.settings.get(module_id, module_settings.HistoryJournalTitle), format_name) - let title = formated_journal_title.length > 0 ? - formated_journal_title : - game.i18n.format("MM.UI.HistoryJournalTitle", format_name) - - return JournalEntry.create({ - name: title, - ownership: { - default: CONST.DOCUMENT_OWNERSHIP_LEVELS.NONE, - [user.id]: CONST.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER // Make the history private and unmodifiable - }, - }).then(journal => { - return user.setFlag(module_id, history_flag, journal.id).then(() => journal); - }); -} - /** * Handle the dialog to send messages to players from NPCs. * The basic dialog is simple : display a few fields selecting which players diff --git a/scripts/settings.mjs b/scripts/settings.mjs index a567e58..1ec85db 100644 --- a/scripts/settings.mjs +++ b/scripts/settings.mjs @@ -1,4 +1,4 @@ -import {module_id} from "./mills_messages.mjs"; +import { module_id } from "./utils.mjs"; export const module_settings = Object.freeze({ MessageDialogTitle: "messageDialogTitle", diff --git a/scripts/utils.mjs b/scripts/utils.mjs new file mode 100644 index 0000000..2504c28 --- /dev/null +++ b/scripts/utils.mjs @@ -0,0 +1,52 @@ +/* + * This file contains some module-wide constants and utility functions + * that do not really fit elsewhere. + */ + +import { module_settings } from "./settings.mjs"; + +export const module_id = "the-mills-messages"; + +// User flag to point to the ID of the journal to store message history to +export const history_flag = "history-journal"; + +/** + * This is a copy of Foundry's internal formatting used by + * localize.format(), so that we can be consistent. + * @param {String} str The string whose "{named_args}" will be formatted + * @param {Object} data An object with named_args members, whose data is replaced + * @returns {String} The formatted string + */ +export function str_format(str, data) { + const fmt = /{[^}]+}/g; + str = str.replace(fmt, k => { + return data[k.slice(1, -1)]; + }); + return str; +} + +/** + * Create the journal entry for the history of the specified user, + * update the user's history flag and return the entry for further + * manipulation if needed. + * @param {User} user The user object that we are creating the history for + * @return {Promise} The JournalEntry that was created. + */ +export async function create_history_journal(user) { + // Use the settings' title for the journal, or revert to the default one if it is empty. + let format_name = {name: user.character ? user.character.name : user.name}; + let formated_journal_title = str_format(game.settings.get(module_id, module_settings.HistoryJournalTitle), format_name) + let title = formated_journal_title.length > 0 ? + formated_journal_title : + game.i18n.format("MM.UI.HistoryJournalTitle", format_name) + + return JournalEntry.create({ + name: title, + ownership: { + default: CONST.DOCUMENT_OWNERSHIP_LEVELS.NONE, + [user.id]: CONST.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER // Make the history private and unmodifiable + }, + }).then(journal => { + return user.setFlag(module_id, history_flag, journal.id).then(() => journal); + }); +}