diff --git a/src/lang/en.json b/src/lang/en.json index 0534959..8b3ec49 100644 --- a/src/lang/en.json +++ b/src/lang/en.json @@ -2,9 +2,10 @@ "SESSION_TITLE_SUGGESTIONS.SETTING_ROLE_HINT": "Select the minimum role required to use the /titles command.", "SESSION_TITLE_SUGGESTIONS.SETTING_ROLE_NAME": "List Titles Command Required Role", "SESSION_TITLE_SUGGESTIONS.INVALID_DATE": "Invalid date provided.", + "SESSION_TITLE_SUGGESTIONS.NO_SUGGESTIONS": "No session title suggestions found.", "SESSION_TITLE_SUGGESTIONS.SUGGESTION_AUTOCOMPLETE_MESSAGE": "Enter your session title suggestion.", "SESSION_TITLE_SUGGESTIONS.SUGGESTION_LIST_COMMAND_DESCRIPTION": "List all title suggestions for the provided date", "SESSION_TITLE_SUGGESTIONS.SUGGESTION_COMMAND_DESCRIPTION": "Suggest a title for this session", "SESSION_TITLE_SUGGESTIONS.SUGGESTION_FLAVOR": "suggesting a session title", "SESSION_TITLE_SUGGESTIONS.SUGGESTION_LIST_FLAVOR": "session title suggestions" -} \ No newline at end of file +} diff --git a/src/scripts/asc-session-title-suggestions.js b/src/scripts/asc-session-title-suggestions.js index 30ef2fa..e1b0509 100644 --- a/src/scripts/asc-session-title-suggestions.js +++ b/src/scripts/asc-session-title-suggestions.js @@ -1,3 +1,4 @@ +const MODULE_ID = "asc-session-title-suggestions" const NAME = "/title" const template_path = "modules/asc-session-title-suggestions/templates" @@ -13,6 +14,24 @@ const TEMPLATES = { } } +function getSuggestionTitle(message) { + return message.getFlag?.(MODULE_ID, "session_title_suggestion") + ?? message.flags?.[MODULE_ID]?.session_title_suggestion + ?? message.flags?.session_title_suggestion +} + +function getSuggestionDate(message) { + return new Date(message.timestamp) +} + +function toSuggestionViewData(message) { + return { + title: getSuggestionTitle(message), + userName: message.user?.name ?? message.author?.name ?? game.users.get(message.userId)?.name ?? "", + timestamp: message.timestamp + } +} + function registerClipboardCopyButton() { copyToClipboardListener = (event) => { let text=event.target.dataset.clipboardText; @@ -39,7 +58,11 @@ function registerCustomChatCommands() { const newMessageData = {} newMessageData.content = await renderTemplate(TEMPLATES.suggestion.content, {content:titleSuggestion}) newMessageData.flavor = await renderTemplate(TEMPLATES.suggestion.flavor) - newMessageData.flags = {session_title_suggestion: titleSuggestion} + newMessageData.flags = { + [MODULE_ID]: { + session_title_suggestion: titleSuggestion + } + } return newMessageData; }, autocompleteCallback: (menu, alias, parameters) => [ @@ -58,34 +81,38 @@ function registerCustomChatCommands() { callback: async (chat, parameters, messageData) => { const newMessageData = {} const suggestions = game.messages.filter(m => { - return m.flags.session_title_suggestion + return getSuggestionTitle(m) }) + if (!suggestions.length) { + ui.notifications.warn(game.i18n.localize("SESSION_TITLE_SUGGESTIONS.NO_SUGGESTIONS") || "No session title suggestions found."); + return; + } let selected_message_date if (!parameters) { - selected_message_date = new Date(suggestions[suggestions.length-1].timestamp) + selected_message_date = getSuggestionDate(suggestions[suggestions.length-1]) } else { const epoch_date = Date.parse(parameters) if (Number.isNaN(epoch_date)){ - ui.notifications.error(`${game.i18n.localize("SESSION_TITLE_SUGGESTIONS.INVALID_DATE")} (${parameter})`); + ui.notifications.error(`${game.i18n.localize("SESSION_TITLE_SUGGESTIONS.INVALID_DATE")} (${parameters})`); return; } selected_message_date = new Date(epoch_date) } const filtered_suggestions = suggestions.filter(m=>{ - const message_date = new Date(m.timestamp) + const message_date = getSuggestionDate(m) return message_date.toDateString() == selected_message_date.toDateString() - }) + }).map(toSuggestionViewData) newMessageData.content = await renderTemplate(TEMPLATES.suggestionList.content, {messages:filtered_suggestions }) newMessageData.flavor = await renderTemplate(TEMPLATES.suggestionList.flavor, {date:selected_message_date.toDateString()}) return newMessageData; }, autocompleteCallback: (menu, alias, parameters) => { const suggestions = game.messages.filter(m => { - return m.flags.session_title_suggestion + return getSuggestionTitle(m) }) const dates = new Set( - suggestions.map(s=>new Date(s.timestamp).toDateString()) + suggestions.map(s=>getSuggestionDate(s).toDateString()) ) const entries = [...dates].map(date=>{ return game.chatCommands.createCommandElement(`${alias} ${date}`, date) @@ -120,11 +147,11 @@ Hooks.on("init", function() { }, hint: game.i18n.localize("SESSION_TITLE_SUGGESTIONS.SETTING_ROLE_HINT") }); - import ('https://cdn.jsdelivr.net/npm/title-case@4.3.1/dist/index.min.js') + import ('https://cdn.jsdelivr.net/npm/title-case@4.3.2/dist/index.min.js') .then((module)=>{console.log('asc','loading...');Handlebars.registerHelper("titlecase", module.titleCase)}) }); Hooks.on("ready", function() { //This code runs once core initialization is ready and game data is available. registerCustomChatCommands(); -}); \ No newline at end of file +}); diff --git a/src/templates/suggestion-list-content.hbs b/src/templates/suggestion-list-content.hbs index 419a057..a779cbf 100644 --- a/src/templates/suggestion-list-content.hbs +++ b/src/templates/suggestion-list-content.hbs @@ -1,3 +1,3 @@