|
1 | | -<script lang="ts" context="module"> |
2 | | - import { TextDisplay } from '../outliner/textDisplay' |
3 | | - import { events } from '../util/events' |
4 | | - import { Valuable } from '../util/stores' |
| 1 | +<script lang="ts"> |
5 | 2 | import { CodeJar } from '@novacbn/svelte-codejar' |
| 3 | + import { onDestroy } from 'svelte' |
| 4 | + import { type Unsubscriber } from 'svelte/store' |
6 | 5 | import { |
7 | | - TEXT_DISPLAY_WIDTH_SLIDER, |
8 | | - TEXT_DISPLAY_BACKGROUND_COLOR_PICKER, |
9 | | - TEXT_DISPLAY_SHADOW_TOGGLE, |
10 | 6 | TEXT_DISPLAY_ALIGNMENT_SELECT, |
| 7 | + TEXT_DISPLAY_BACKGROUND_COLOR_PICKER, |
11 | 8 | TEXT_DISPLAY_SEE_THROUGH_TOGGLE, |
| 9 | + TEXT_DISPLAY_SHADOW_TOGGLE, |
| 10 | + TEXT_DISPLAY_WIDTH_SLIDER, |
12 | 11 | } from '../interface/panel/textDisplayElement' |
| 12 | + import { TextDisplay } from '../outliner/textDisplay' |
| 13 | + import { events } from '../util/events' |
13 | 14 | import { floatToHex } from '../util/misc' |
| 15 | + import { Valuable } from '../util/stores' |
14 | 16 | import { translate } from '../util/translation' |
15 | 17 |
|
16 | 18 | function highlight(code: string, syntax?: string) { |
17 | 19 | if (!syntax) return code |
18 | 20 | return Prism.highlight(code, Prism.languages[syntax], syntax) |
19 | 21 | } |
20 | | -</script> |
21 | 22 |
|
22 | | -<script lang="ts"> |
| 23 | + let lastSelected: TextDisplay | undefined |
23 | 24 | let selected = TextDisplay.selected.at(0) |
24 | 25 |
|
25 | 26 | // @ts-expect-error |
26 | 27 | let text = selected?._text ?? new Valuable('') |
| 28 | + let lastText: string | undefined = selected?.text |
27 | 29 | // @ts-expect-error |
28 | 30 | let error = selected?._textError ?? new Valuable('') |
29 | 31 |
|
|
34 | 36 | let seeThroughSlot: HTMLDivElement |
35 | 37 | let codeJar: CodeJar |
36 | 38 |
|
37 | | - events.UPDATE_SELECTION.subscribe(() => { |
| 39 | + let unsubFromText: Unsubscriber | undefined |
| 40 | +
|
| 41 | + const unsubFromEvent = events.UPDATE_SELECTION.subscribe(() => { |
| 42 | + unsubFromText?.() |
| 43 | +
|
| 44 | + lastSelected = selected |
38 | 45 | selected = TextDisplay.selected.at(0) |
39 | 46 | if (!selected) return |
40 | 47 | // This might be a bit hacky, but svelte seems to handle it fine. |
41 | 48 | // @ts-ignore |
42 | 49 | text = selected._text |
| 50 | + lastText = selected.text |
43 | 51 | error = selected.textError |
44 | 52 |
|
45 | 53 | // Force the inputs to update |
|
49 | 57 | TEXT_DISPLAY_SHADOW_TOGGLE.set(selected.shadow) |
50 | 58 | TEXT_DISPLAY_ALIGNMENT_SELECT.set(selected.align) |
51 | 59 | TEXT_DISPLAY_SEE_THROUGH_TOGGLE.set(selected.seeThrough) |
| 60 | +
|
| 61 | + unsubFromText = text.subscribe(value => { |
| 62 | + if (!Project) return |
| 63 | + if (lastSelected !== selected || lastSelected === undefined) { |
| 64 | + lastSelected = selected |
| 65 | + return |
| 66 | + } |
| 67 | + if (lastText === value) return |
| 68 | + console.log('Text changed') |
| 69 | + lastText = value |
| 70 | + Project.saved = false |
| 71 | + }) |
52 | 72 | }) |
53 | 73 |
|
54 | 74 | requestAnimationFrame(() => { |
|
65 | 85 | codeJar.$$.ctx[0].style.overflowWrap = 'unset' |
66 | 86 | codeJar.$$.ctx[0].style.whiteSpace = 'nowrap' |
67 | 87 | } |
| 88 | +
|
| 89 | + onDestroy(() => { |
| 90 | + unsubFromEvent() |
| 91 | + unsubFromText?.() |
| 92 | + }) |
68 | 93 | </script> |
69 | 94 |
|
70 | 95 | <p class="panel_toolbar_label label" style={!!selected ? '' : 'visibility:hidden; height: 0px;'}> |
|
0 commit comments