-
Notifications
You must be signed in to change notification settings - Fork 1
Selection export + toolbar changes #63
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d43d80d
add option to export only selected text
Silabear 7d5d82a
add settings option to hide export this button
Silabear c6b3ad1
fix modal errors
Silabear 209fc8a
shrink max output height
Silabear 3781057
minor toolbar changes
Silabear ef1ffbc
on second thoughts, this makes no sense
Silabear 97d73a8
fix tests
Silabear f5bc359
Merge branch 'main' into feature/selection-export
Silabear 937f136
remove commented out code
Silabear File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <script lang="ts"> | ||
| import Modal from "$lib/components/Modal.svelte"; | ||
| import { Editor } from "@tiptap/core"; | ||
| import Key from "../Key.svelte"; | ||
| import MiniRenderer from "../text/MiniRenderer.svelte"; | ||
| import { appSettings } from "$lib/settings"; | ||
| import { convert } from "$lib/text/nbt/export"; | ||
| import { json, typescript } from "svelte-highlight/languages"; | ||
| import Highlight from "svelte-highlight"; | ||
|
|
||
| import IconCopy from "~icons/tabler/copy"; | ||
| import IconCheck from "~icons/tabler/check"; | ||
| import CheckBox from "../CheckBox.svelte"; | ||
|
|
||
| let recentlyCopied: boolean = $state(false) | ||
| let jsonOutput: boolean = $state(false) | ||
|
|
||
| let { | ||
| exportSelectionDialog = $bindable(), | ||
| editor, | ||
| shouldOptimise = true | ||
| }: { | ||
| exportSelectionDialog: Modal, | ||
| editor: Editor, | ||
| shouldOptimise: boolean | ||
| } = $props(); | ||
|
|
||
| let value: object = $state({}) | ||
|
|
||
| function onOpen() { | ||
| let content = editor.state.doc.slice(editor.state.selection.from, editor.state.selection.to).content.toJSON() | ||
|
|
||
| // Fix if only one line | ||
| if (content[0].type !== "paragraph") { | ||
| content = [{ | ||
| type: "paragraph", | ||
| content: content | ||
| }] | ||
| } | ||
|
|
||
| // If last paragraph is empty, remove | ||
| if (content.length > 0 && !("content" in content[content.length - 1])) { | ||
| content.pop(); | ||
| } | ||
|
|
||
| // Format in document | ||
| value = { | ||
| type: "doc", | ||
| content: content | ||
| }; | ||
| } | ||
| </script> | ||
|
|
||
| <Modal title="Export selection" bind:this={exportSelectionDialog} key="Q" onOpen={onOpen}> | ||
| <div class="flex w-full flex-col space-y-1"> | ||
| {#key value} | ||
| <p class="w-full">Selected text:</p> | ||
| <div class="bg-zinc-900 rounded-md px-1"> | ||
| <MiniRenderer value={value} /> | ||
| </div> | ||
|
|
||
| <p class="w-full mt-2">Output:</p> | ||
| <code class="max-h-56 w-full space-x-3 overflow-auto rounded-lg bg-zinc-950 p-3"> | ||
| {#if $appSettings.syntaxHighlight} | ||
| <Highlight | ||
| language={jsonOutput ? json : typescript} | ||
| code={ | ||
| convert( | ||
| value, | ||
| shouldOptimise, | ||
| "standard", | ||
| jsonOutput, | ||
| ) | ||
| } /> | ||
| {:else} | ||
| <pre class="inline break-all whitespace-pre-wrap"> | ||
| {editor ? convert( | ||
| value, | ||
| shouldOptimise, | ||
| "standard", | ||
| jsonOutput | ||
| ) : "Loading..."} | ||
| </pre> | ||
| {/if} | ||
| </code> | ||
|
|
||
| <div class="flex items-center space-x-2 mb-2 mt-1"> | ||
| <CheckBox label="bg" bind:value={jsonOutput} /> | ||
| <label for="bg">JSON output</label> | ||
| </div> | ||
|
|
||
| <button | ||
| class="btn flex items-center space-x-2" | ||
| onclick={() => { | ||
| navigator.clipboard.writeText( | ||
| convert( | ||
| value, | ||
| shouldOptimise | ||
| ) | ||
| ); | ||
| recentlyCopied = true; | ||
| setTimeout(() => (recentlyCopied = false), 2000); | ||
| }}> | ||
| {#if !recentlyCopied} | ||
| <IconCopy /> | ||
| <span>Copy</span> | ||
| {:else} | ||
| <IconCheck /> | ||
| <span>Copied!</span> | ||
| {/if} | ||
| </button> | ||
|
|
||
| {/key} | ||
| </div> | ||
| </Modal> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| import { Extension } from '@tiptap/core'; | ||
| import { Plugin, PluginKey } from '@tiptap/pm/state'; | ||
| import { EditorView } from '@tiptap/pm/view'; | ||
| import { appSettings } from "$lib/settings"; | ||
| import { get } from 'svelte/store'; | ||
|
|
||
| // Define the shape of configuration options for type safety | ||
| export interface ExportButtonOptions { | ||
| onClick: () => void; | ||
| } | ||
|
|
||
| export const ExportButtonExtension = Extension.create<ExportButtonOptions>({ | ||
| name: 'ExportButton', | ||
|
|
||
| addOptions() { | ||
| return { | ||
| onClick: () => { }, | ||
| }; | ||
| }, | ||
|
|
||
| addProseMirrorPlugins() { | ||
| const { onClick } = this.options; | ||
| let buttonDom: HTMLButtonElement | null = null; | ||
|
|
||
| return [ | ||
| new Plugin({ | ||
| key: new PluginKey('ExportButton'), | ||
| view(editorView: EditorView) { | ||
| return { | ||
| update(view: EditorView) { | ||
| if (get(appSettings).hideSelectionExport == true) { | ||
| return; | ||
| } | ||
|
|
||
| const { state } = view; | ||
| const { selection } = state; | ||
|
|
||
| const parentNode = view.dom.parentNode as HTMLElement | null; | ||
| if (!parentNode) return; | ||
|
|
||
| if ((selection.empty || !view.hasFocus()) && buttonDom) { | ||
| buttonDom.style.display = 'none'; | ||
| return; | ||
| } | ||
|
|
||
| if (!buttonDom) { | ||
| buttonDom = document.createElement('button'); | ||
| buttonDom.innerText = '↪ Export this'; | ||
| buttonDom.className = 'export-button'; | ||
| buttonDom.style.position = 'absolute'; | ||
| buttonDom.style.zIndex = '10'; | ||
|
|
||
| buttonDom.addEventListener('mousedown', (e: MouseEvent) => e.preventDefault()); | ||
| buttonDom.addEventListener('click', onClick); | ||
|
|
||
| parentNode.appendChild(buttonDom); | ||
| } | ||
|
|
||
| const coords = view.coordsAtPos(selection.to); | ||
| const top = coords.top + 19; | ||
| const left = coords.left - 90; | ||
|
|
||
| buttonDom.style.display = 'block'; | ||
| buttonDom.style.top = `${top}px`; | ||
| buttonDom.style.left = `${left + 5}px`; | ||
| }, | ||
|
|
||
| destroy() { | ||
| if (buttonDom) { | ||
| buttonDom.remove(); | ||
| buttonDom = null; | ||
| } | ||
| } | ||
| }; | ||
| }, | ||
| }), | ||
| ]; | ||
| }, | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.