Add copy button to Star Wars Style Journal
- Added a copy button to the Star Wars Style Journal sheet. - The copy button allows users to easily copy the content of the journal (in markdown format via turndown) to their clipboard. - A notification is displayed upon successful copying.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
var turndownService = new TurndownService()
|
||||
|
||||
export class StarWarsStyleJournalSheet extends JournalSheet {
|
||||
static get defaultOptions() {
|
||||
return mergeObject(super.defaultOptions, {
|
||||
@@ -7,6 +9,8 @@ export class StarWarsStyleJournalSheet extends JournalSheet {
|
||||
async _render(force, options) {
|
||||
await super._render(force, {...options, collapsed:true});
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
export class StarWarsTextPageSheet extends JournalTextPageSheet {
|
||||
@@ -18,4 +22,33 @@ export class StarWarsTextPageSheet extends JournalTextPageSheet {
|
||||
resizable: true,
|
||||
});
|
||||
}
|
||||
activateListeners(html) {
|
||||
super.activateListeners(html);
|
||||
// Ensure the button is not duplicated
|
||||
if (html.find(".asc-copy-button").length > 0) return;
|
||||
|
||||
// Create the button element
|
||||
const button = $(`<div class="asc-copy"><button><i class="fas fa-clipboard"></i>Copy</button></div>`);
|
||||
|
||||
// Insert it inside the .journal-header
|
||||
const journal_header = html.find(".journal-header aside")
|
||||
journal_header.append(button);
|
||||
|
||||
// Attach event listener
|
||||
button.click(this._onCopyButtonClick.bind(this));
|
||||
}
|
||||
|
||||
_onCopyButtonClick(event) {
|
||||
event.preventDefault();
|
||||
var editorContent = this.element.find(".editor-content").html();
|
||||
|
||||
// Replace linked items
|
||||
editorContent = editorContent.replace(/@\w+\[.+\]\{(.*?)\}/g,"$1")
|
||||
|
||||
const markdownContent = turndownService.turndown(editorContent)
|
||||
|
||||
// Copy to clipboard
|
||||
navigator.clipboard.writeText(markdownContent)
|
||||
.then(()=>ui.notifications.info("Journal content copied as Markdown!"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user