@sdeck/plugin-spotify 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1,18 +1,23 @@
1
1
  // @bun
2
2
  // src/index.tsx
3
3
  import {
4
- defineAction,
5
- definePlugin,
6
- defineTrigger,
4
+ createPlugin,
5
+ following,
6
+ Permissions,
7
7
  Shared,
8
- useKeyPress,
9
- useSensorPress
8
+ setting,
9
+ usePress
10
10
  } from "@sdeck/plugin";
11
11
  import {
12
+ captionLayer,
12
13
  iconLayer,
13
14
  iconLook,
14
- textLayer
15
+ iconSize,
16
+ meterLayer,
17
+ surface,
18
+ volumeStepLayers
15
19
  } from "@sdeck/schema";
20
+ import { z } from "zod";
16
21
 
17
22
  // src/api.ts
18
23
  import { createHash, randomBytes } from "crypto";
@@ -305,6 +310,7 @@ function parseDevices(body) {
305
310
 
306
311
  // src/panels.tsx
307
312
  import { useHostState, useImage } from "@sdeck/plugin";
313
+ import { surfaceStyle } from "@sdeck/schema";
308
314
  import { jsx, jsxs } from "react/jsx-runtime";
309
315
  var GREEN = "#1ed760";
310
316
  var BACKGROUND = "#121212";
@@ -356,7 +362,7 @@ function NowPlaying({ kind }) {
356
362
  width: "100%",
357
363
  height: "100%",
358
364
  alignItems: "center",
359
- background: BACKGROUND,
365
+ ...surfaceStyle(BACKGROUND),
360
366
  gap: 4,
361
367
  padding: "0 4px"
362
368
  },
@@ -386,7 +392,7 @@ function NowPlaying({ kind }) {
386
392
  alignItems: "center",
387
393
  justifyContent: "center",
388
394
  gap: 8,
389
- background: BACKGROUND
395
+ ...surfaceStyle(BACKGROUND)
390
396
  },
391
397
  children: [
392
398
  /* @__PURE__ */ jsx(Icon, {
@@ -395,7 +401,7 @@ function NowPlaying({ kind }) {
395
401
  color: GREEN
396
402
  }),
397
403
  /* @__PURE__ */ jsx("div", {
398
- style: { ...box, color: MUTED, fontSize: kind === "window" ? 15 : 11, fontWeight: 700 },
404
+ style: { ...box, color: MUTED, fontSize: kind === "window" ? 15 : 12, fontWeight: 700 },
399
405
  children: "Not playing"
400
406
  })
401
407
  ]
@@ -408,7 +414,7 @@ function NowPlaying({ kind }) {
408
414
  width: "100%",
409
415
  height: "100%",
410
416
  flexDirection: "column",
411
- background: BACKGROUND
417
+ ...surfaceStyle(BACKGROUND)
412
418
  },
413
419
  children: [
414
420
  /* @__PURE__ */ jsxs("div", {
@@ -491,7 +497,7 @@ function NowPlaying({ kind }) {
491
497
  width: "100%",
492
498
  height: "100%",
493
499
  position: "relative",
494
- background: BACKGROUND
500
+ ...surfaceStyle(BACKGROUND)
495
501
  },
496
502
  children: [
497
503
  art ? /* @__PURE__ */ jsx("img", {
@@ -570,15 +576,31 @@ function NowPlaying({ kind }) {
570
576
  // src/index.tsx
571
577
  import { jsx as jsx2 } from "react/jsx-runtime";
572
578
  var DEFAULT_PORT = 43821;
579
+ var plugin = createPlugin({
580
+ id: "@sdeck/plugin-spotify",
581
+ name: "Spotify",
582
+ version: "0.4.0",
583
+ namespace: "spotify",
584
+ description: "Now playing, playback, Liked Songs, playlists and devices through the Spotify Web API.",
585
+ author: "sdeck",
586
+ permissions: [Permissions.network],
587
+ settings: {
588
+ clientId: setting.text("Client ID", {
589
+ required: true,
590
+ validate: z.string().trim().regex(/^[0-9a-f]{32}$/i, "should be 32 letters and digits, from your app's settings"),
591
+ description: "Create an app at developer.spotify.com and add the redirect URI http://127.0.0.1:43821/callback (with your port)."
592
+ }),
593
+ port: setting.number("Sign-in port", { default: DEFAULT_PORT, min: 1024, max: 65535 })
594
+ }
595
+ });
573
596
  var POLL_MS = 3000;
574
- var context;
575
597
  var signingIn;
576
598
  var spotify = new SpotifyClient({
577
- clientId: () => String(context?.settings().clientId ?? "").trim(),
599
+ clientId: () => plugin.context?.settings().clientId.trim() ?? "",
578
600
  tokens: {
579
- get: () => context?.secrets.get("refreshToken"),
601
+ get: () => plugin.context?.secrets.get("refreshToken"),
580
602
  set: async (token) => {
581
- await context?.secrets.set("refreshToken", token);
603
+ await plugin.context?.secrets.set("refreshToken", token);
582
604
  }
583
605
  }
584
606
  });
@@ -602,7 +624,7 @@ var player = new Shared((emit) => {
602
624
  const left = next.durationMs - next.positionMs;
603
625
  if (next.playing && left > 0)
604
626
  wait = Math.min(wait, left + 400);
605
- context?.status("Signed in to Spotify", "ok");
627
+ plugin.context?.status("Signed in to Spotify", "ok");
606
628
  } catch (error) {
607
629
  if (!stopped)
608
630
  emit(undefined);
@@ -610,7 +632,7 @@ var player = new Shared((emit) => {
610
632
  wait = error.retryAfterMs;
611
633
  else if (error instanceof SignedOut)
612
634
  wait = 1e4;
613
- context?.status(error.message, error instanceof SignedOut ? "info" : "error");
635
+ plugin.context?.status(error.message, error instanceof SignedOut ? "info" : "error");
614
636
  }
615
637
  running = false;
616
638
  if (!stopped)
@@ -698,14 +720,53 @@ async function playlists() {
698
720
  }
699
721
  return all;
700
722
  }
723
+ var playlistOptions = async (writable) => (await playlists()).filter((p) => !writable || p.writable).map((p) => ({ value: writable ? p.id : p.uri, label: p.name }));
724
+ var sources = {
725
+ playlists: plugin.source("playlists", () => playlistOptions(true)),
726
+ library: plugin.source("library", () => playlistOptions(false)),
727
+ devices: plugin.source("devices", async () => parseDevices(await spotify.request("GET", "/me/player/devices")).map((d) => ({
728
+ value: d.id,
729
+ label: `${d.name}${d.active ? " (playing)" : ""}`
730
+ })))
731
+ };
732
+ var from = (pick) => ({ start: following(player, pick) });
733
+ var values = {
734
+ playing: plugin.state.boolean("playing", "Playing", from((p) => p.playing)),
735
+ track: plugin.state.string("track", "Track", from((p) => p.track)),
736
+ artist: plugin.state.string("artist", "Artist", from((p) => p.artist)),
737
+ album: plugin.state.string("album", "Album", from((p) => p.album)),
738
+ art: plugin.state.string("art", "Cover art", from((p) => p.art)),
739
+ progress: plugin.state.number("progress", "Progress", from((p) => p.progress)),
740
+ position: plugin.state.number("position", "Position", {
741
+ unit: "s",
742
+ ...from((p) => p.position)
743
+ }),
744
+ duration: plugin.state.number("duration", "Duration", {
745
+ unit: "s",
746
+ ...from((p) => p.duration)
747
+ }),
748
+ startedAt: plugin.state.number("startedAt", "Started at", {
749
+ description: "When the track would have started to be where it is now, in ms since 1970. Only while playing: now() - spotify.startedAt is the exact position.",
750
+ ...from((p) => p.startedAt)
751
+ }),
752
+ volume: plugin.state.number("volume", "Volume", from((p) => p.volume)),
753
+ shuffle: plugin.state.boolean("shuffle", "Shuffle", from((p) => p.shuffle)),
754
+ repeat: plugin.state.string("repeat", "Repeat", from((p) => p.repeat)),
755
+ device: plugin.state.string("device", "Device", from((p) => p.device)),
756
+ uri: plugin.state.string("uri", "Track URI", from((p) => p.uri)),
757
+ liked: plugin.state.boolean("liked", "Liked", {
758
+ description: "Whether what is playing is in Liked Songs.",
759
+ start: following(liked, (is) => is)
760
+ })
761
+ };
701
762
  var key = (icon, caption = "", color = "#ffffff") => iconLook({ icon, caption, background: BACKGROUND, color });
702
763
  var dot = (active) => iconLayer("dot", disc(GREEN), { size: 7, position: "bottom", visible: active });
703
764
  var switched = (active, on, off, colors) => [
704
- iconLayer("on", on, { color: colors.on, size: 58, visible: active }),
705
- iconLayer("off", off, { color: colors.off, size: 58, visible: `!(${active})` })
765
+ iconLayer("on", on, { color: colors.on, size: iconSize(false), visible: active }),
766
+ iconLayer("off", off, { color: colors.off, size: iconSize(false), visible: `!(${active})` })
706
767
  ];
707
768
  var setPlaying = (play) => command("PUT", play ? "/me/player/play" : "/me/player/pause");
708
- var playPause = defineAction({
769
+ var playPause = plugin.action({
709
770
  id: "play-pause",
710
771
  name: "Play / pause",
711
772
  description: "Shows the cover of what is playing; pressing it plays or pauses.",
@@ -713,7 +774,7 @@ var playPause = defineAction({
713
774
  icon: "play",
714
775
  slots: ["key", "sensor"],
715
776
  settings: {
716
- cover: { type: "boolean", label: "Show the cover art", default: true, appearance: true }
777
+ cover: setting.boolean("Show the cover art", { default: true, appearance: true })
717
778
  },
718
779
  run: async () => {
719
780
  const before = (await readPlayer()).playing;
@@ -730,34 +791,37 @@ var playPause = defineAction({
730
791
  iconLayer("pause", "tb/TbPlayerPauseFilled", {
731
792
  color: "#000000",
732
793
  frame: centered(34),
733
- visible: `(${visible}) && spotify.playing`
794
+ visible: `(${visible}) && ${values.playing.key}`
734
795
  }),
735
796
  iconLayer("play", "tb/TbPlayerPlayFilled", {
736
797
  color: "#000000",
737
798
  frame: { ...centered(34), x: 35 },
738
- visible: `(${visible}) && !spotify.playing`
799
+ visible: `(${visible}) && !${values.playing.key}`
739
800
  })
740
801
  ];
741
802
  if (!s.cover)
742
- return { background: BACKGROUND, layers: button("true") };
803
+ return { background: surface(BACKGROUND), layers: button("true") };
743
804
  return {
744
- background: BACKGROUND,
805
+ background: surface(BACKGROUND),
745
806
  layers: [
746
- iconLayer("cover", "{{spotify.art}}", { frame: centered(100), visible: "spotify.art" }),
807
+ iconLayer("cover", values.art.template, {
808
+ frame: centered(100),
809
+ visible: values.art.key
810
+ }),
747
811
  iconLayer("shade", disc("#000000", 0.6), {
748
812
  frame: centered(48),
749
- visible: "spotify.art && !spotify.playing"
813
+ visible: `${values.art.key} && !${values.playing.key}`
750
814
  }),
751
815
  iconLayer("resume", "tb/TbPlayerPlayFilled", {
752
816
  frame: { ...centered(24), x: 39 },
753
- visible: "spotify.art && !spotify.playing"
817
+ visible: `${values.art.key} && !${values.playing.key}`
754
818
  }),
755
- ...button("!spotify.art")
819
+ ...button(`!${values.art.key}`)
756
820
  ]
757
821
  };
758
822
  }
759
823
  });
760
- var skip = (id, name, icon, back) => defineAction({
824
+ var skip = (id, name, icon, back) => plugin.action({
761
825
  id,
762
826
  name,
763
827
  category: "Spotify",
@@ -771,7 +835,7 @@ var skip = (id, name, icon, back) => defineAction({
771
835
  });
772
836
  var next = skip("next", "Next track", "skip-forward", "previous");
773
837
  var previous = skip("previous", "Previous track", "skip-back", "next");
774
- var nowPlaying = defineAction({
838
+ var nowPlaying = plugin.action({
775
839
  id: "now-playing",
776
840
  name: "Now playing",
777
841
  description: "The current track and artist, with how far through it is.",
@@ -779,17 +843,12 @@ var nowPlaying = defineAction({
779
843
  icon: "music",
780
844
  slots: ["window", "key", "sensor"],
781
845
  settings: {
782
- press: {
783
- type: "select",
784
- label: "When pressed",
785
- default: "play-pause",
786
- options: [
787
- { value: "play-pause", label: "Play or pause" },
788
- { value: "next", label: "Next track" },
789
- { value: "like", label: "Like or unlike" },
790
- { value: "none", label: "Nothing" }
791
- ]
792
- }
846
+ press: setting.select("When pressed", {
847
+ "play-pause": "Play or pause",
848
+ next: "Next track",
849
+ like: "Like or unlike",
850
+ none: "Nothing"
851
+ })
793
852
  },
794
853
  display: true,
795
854
  run: async () => ({ result: await readPlayer() }),
@@ -801,52 +860,38 @@ var nowPlaying = defineAction({
801
860
  const done = press === "next" ? command("POST", "/me/player/next") : press === "like" ? current().then(async ({ uri }) => setLiked(uri, !await isLiked(uri))) : readPlayer().then((p) => setPlaying(!p.playing));
802
861
  done.catch((error) => plugin.log("now playing:", error.message));
803
862
  };
804
- useKeyPress(slot.kind === "key" ? act : undefined, {
805
- key: slot.kind === "key" ? slot.index : -1
806
- });
807
- useSensorPress(slot.kind === "sensor" ? act : undefined, {
808
- sensor: slot.kind === "sensor" ? slot.index : -1
809
- });
863
+ usePress(act);
810
864
  return /* @__PURE__ */ jsx2(NowPlaying, {
811
865
  kind: slot.kind
812
866
  });
813
867
  }
814
868
  });
815
869
  var unmuted = 50;
816
- var volume = defineAction({
870
+ var volume = plugin.action({
817
871
  id: "volume",
818
872
  name: "Spotify volume",
819
873
  category: "Spotify",
820
874
  icon: "volume-1",
821
875
  slots: ["key", "sensor"],
822
876
  settings: {
823
- operation: {
824
- type: "select",
825
- label: "When pressed",
826
- default: "up",
827
- options: [
828
- { value: "up", label: "Volume up" },
829
- { value: "down", label: "Volume down" },
830
- { value: "set", label: "Set a level" },
831
- { value: "mute", label: "Mute / unmute" }
832
- ]
833
- },
834
- step: {
835
- type: "number",
836
- label: "Step (%)",
877
+ operation: setting.select("When pressed", {
878
+ up: "Volume up",
879
+ down: "Volume down",
880
+ set: "Set a level",
881
+ mute: "Mute / unmute"
882
+ }),
883
+ step: setting.number("Step (%)", {
837
884
  default: 10,
838
885
  min: 1,
839
886
  max: 50,
840
887
  visibleIf: { operation: ["up", "down"] }
841
- },
842
- level: {
843
- type: "number",
844
- label: "Level (%)",
888
+ }),
889
+ level: setting.number("Level (%)", {
845
890
  default: 50,
846
891
  min: 0,
847
892
  max: 100,
848
893
  visibleIf: { operation: "set" }
849
- }
894
+ })
850
895
  },
851
896
  run: async ({ settings }) => {
852
897
  const before = (await readPlayer()).volume;
@@ -861,44 +906,38 @@ var volume = defineAction({
861
906
  await set(level);
862
907
  return { result: level, label: `Spotify volume ${level}%`, undo: () => set(before) };
863
908
  },
864
- look: (s) => {
865
- const icon = s.operation === "mute" ? "lucide/volume-x" : s.operation === "down" ? "lucide/volume-1" : "lucide/volume-2";
866
- const level = {
867
- id: "level",
868
- type: "bar",
869
- value: "spotify.volume ?? 0",
870
- color: GREEN,
871
- track: FAINT,
872
- position: "bottom",
873
- thickness: 6
874
- };
875
- if (s.operation === "mute") {
876
- return {
877
- background: BACKGROUND,
878
- layers: [
879
- ...switched("spotify.volume == 0", icon, "lucide/volume-2", {
880
- on: "#f87171",
881
- off: "#ffffff"
882
- }),
883
- level
884
- ]
885
- };
909
+ look: (s, slot) => {
910
+ const background = surface(BACKGROUND);
911
+ const level = meterLayer("level", `${values.volume.key} ?? 0`, { color: GREEN });
912
+ switch (s.operation) {
913
+ case "mute":
914
+ return {
915
+ background,
916
+ layers: [
917
+ ...switched(`${values.volume.key} == 0`, "lucide/volume-x", "lucide/volume-2", {
918
+ on: "#f87171",
919
+ off: "#ffffff"
920
+ }),
921
+ level
922
+ ]
923
+ };
924
+ case "set":
925
+ return {
926
+ background,
927
+ layers: [
928
+ iconLayer("icon", "lucide/volume-2", { color: "#ffffff", size: 42 }),
929
+ captionLayer("label", `${s.level}%`, { color: MUTED, size: 14 }),
930
+ level
931
+ ]
932
+ };
933
+ default: {
934
+ const step = s.operation === "up" ? "plus" : "minus";
935
+ return { background, layers: [...volumeStepLayers(step, slot, { center: 46 }), level] };
936
+ }
886
937
  }
887
- return {
888
- background: BACKGROUND,
889
- layers: [
890
- iconLayer("icon", icon, {
891
- color: "#ffffff",
892
- size: s.operation === "set" ? 42 : 52,
893
- position: s.operation === "set" ? "top" : "middle"
894
- }),
895
- ...s.operation === "set" ? [textLayer("label", `${s.level}%`, { color: MUTED, size: 14, position: "bottom" })] : [],
896
- level
897
- ]
898
- };
899
938
  }
900
939
  });
901
- var shuffle = defineAction({
940
+ var shuffle = plugin.action({
902
941
  id: "shuffle",
903
942
  name: "Shuffle",
904
943
  category: "Spotify",
@@ -911,20 +950,21 @@ var shuffle = defineAction({
911
950
  return { result: !before, label: `Shuffle ${before ? "off" : "on"}`, undo: () => set(before) };
912
951
  },
913
952
  look: () => ({
914
- background: BACKGROUND,
953
+ background: surface(BACKGROUND),
915
954
  layers: [
916
- ...switched("spotify.shuffle", "lucide/shuffle", "lucide/shuffle", {
955
+ ...switched(values.shuffle.key, "lucide/shuffle", "lucide/shuffle", {
917
956
  on: GREEN,
918
957
  off: MUTED
919
958
  }),
920
- dot("spotify.shuffle")
959
+ dot(values.shuffle.key)
921
960
  ]
922
961
  })
923
962
  });
924
963
  function nextRepeat(mode) {
925
964
  return mode === "off" ? "context" : mode === "context" ? "track" : "off";
926
965
  }
927
- var repeat = defineAction({
966
+ var repeating = `${values.repeat.key} == "context" || ${values.repeat.key} == "track"`;
967
+ var repeat = plugin.action({
928
968
  id: "repeat",
929
969
  name: "Repeat",
930
970
  description: "Cycles repeat: off, the playlist, one track.",
@@ -939,28 +979,28 @@ var repeat = defineAction({
939
979
  return { result: state, label: `Repeat ${state}`, undo: () => set(before) };
940
980
  },
941
981
  look: () => ({
942
- background: BACKGROUND,
982
+ background: surface(BACKGROUND),
943
983
  layers: [
944
984
  iconLayer("off", "lucide/repeat", {
945
985
  color: MUTED,
946
- size: 58,
947
- visible: 'spotify.repeat != "context" && spotify.repeat != "track"'
986
+ size: iconSize(false),
987
+ visible: `!(${repeating})`
948
988
  }),
949
989
  iconLayer("all", "lucide/repeat", {
950
990
  color: GREEN,
951
- size: 58,
952
- visible: 'spotify.repeat == "context"'
991
+ size: iconSize(false),
992
+ visible: `${values.repeat.key} == "context"`
953
993
  }),
954
994
  iconLayer("one", "lucide/repeat-1", {
955
995
  color: GREEN,
956
- size: 58,
957
- visible: 'spotify.repeat == "track"'
996
+ size: iconSize(false),
997
+ visible: `${values.repeat.key} == "track"`
958
998
  }),
959
- dot('spotify.repeat == "context" || spotify.repeat == "track"')
999
+ dot(repeating)
960
1000
  ]
961
1001
  })
962
1002
  });
963
- var like = defineAction({
1003
+ var like = plugin.action({
964
1004
  id: "like",
965
1005
  name: "Like",
966
1006
  description: "Adds what is playing to Liked Songs, or takes it out. Lit while it is liked.",
@@ -968,16 +1008,11 @@ var like = defineAction({
968
1008
  icon: "heart",
969
1009
  slots: ["key", "sensor"],
970
1010
  settings: {
971
- mode: {
972
- type: "select",
973
- label: "When pressed",
974
- default: "toggle",
975
- options: [
976
- { value: "toggle", label: "Like / unlike" },
977
- { value: "like", label: "Like" },
978
- { value: "unlike", label: "Unlike" }
979
- ]
980
- }
1011
+ mode: setting.select("When pressed", {
1012
+ toggle: "Like / unlike",
1013
+ like: "Like",
1014
+ unlike: "Unlike"
1015
+ })
981
1016
  },
982
1017
  run: async ({ settings }) => {
983
1018
  const { uri, track } = await current();
@@ -992,14 +1027,14 @@ var like = defineAction({
992
1027
  };
993
1028
  },
994
1029
  look: () => ({
995
- background: BACKGROUND,
996
- layers: switched("spotify.liked == true", "tb/TbHeartFilled", "tb/TbHeart", {
1030
+ background: surface(BACKGROUND),
1031
+ layers: switched(`${values.liked.key} == true`, "tb/TbHeartFilled", "tb/TbHeart", {
997
1032
  on: GREEN,
998
1033
  off: MUTED
999
1034
  })
1000
1035
  })
1001
1036
  });
1002
- var addToPlaylist = defineAction({
1037
+ var addToPlaylist = plugin.action({
1003
1038
  id: "add-to-playlist",
1004
1039
  name: "Add to playlist",
1005
1040
  description: "Adds what is playing to one of your playlists.",
@@ -1007,18 +1042,13 @@ var addToPlaylist = defineAction({
1007
1042
  icon: "list-plus",
1008
1043
  slots: ["key", "sensor"],
1009
1044
  settings: {
1010
- playlist: {
1011
- type: "select",
1012
- label: "Playlist",
1013
- options: [],
1014
- source: "playlists",
1045
+ playlist: setting.pick("Playlist", sources.playlists, {
1046
+ required: true,
1015
1047
  description: "Yours, and ones you collaborate on."
1016
- },
1017
- text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true }
1048
+ }),
1049
+ text: setting.text("Text", { placeholder: "Shown under the icon", appearance: true })
1018
1050
  },
1019
1051
  run: async ({ settings }) => {
1020
- if (!settings.playlist)
1021
- throw new Error("Pick a playlist in the key's settings");
1022
1052
  const { uri, track } = await current();
1023
1053
  await spotify.request("POST", `/playlists/${encodeURIComponent(settings.playlist)}/items`, {}, {
1024
1054
  uris: [uri]
@@ -1027,7 +1057,7 @@ var addToPlaylist = defineAction({
1027
1057
  },
1028
1058
  look: (s) => key("lucide/list-plus", s.text)
1029
1059
  });
1030
- var play = defineAction({
1060
+ var play = plugin.action({
1031
1061
  id: "play",
1032
1062
  name: "Play",
1033
1063
  description: "Plays a playlist, album, artist, track or episode, or adds it to the queue.",
@@ -1035,29 +1065,19 @@ var play = defineAction({
1035
1065
  icon: "list-music",
1036
1066
  slots: ["key", "sensor"],
1037
1067
  settings: {
1038
- item: {
1039
- type: "text",
1040
- label: "What to play",
1041
- source: "library",
1068
+ item: setting.text("What to play", {
1069
+ source: sources.library.id,
1070
+ required: true,
1071
+ validate: (item) => spotifyUri(item) ? undefined : "should be a Spotify link or spotify: URI",
1042
1072
  placeholder: "A Spotify link, or pick a playlist",
1043
1073
  description: "In Spotify: Share \u2192 Copy link."
1044
- },
1045
- mode: {
1046
- type: "select",
1047
- label: "When pressed",
1048
- default: "play",
1049
- options: [
1050
- { value: "play", label: "Play now" },
1051
- { value: "queue", label: "Add to the queue (tracks and episodes)" }
1052
- ]
1053
- },
1054
- shuffle: {
1055
- type: "boolean",
1056
- label: "Shuffle",
1057
- default: false,
1058
- visibleIf: { mode: "play" }
1059
- },
1060
- text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true }
1074
+ }),
1075
+ mode: setting.select("When pressed", {
1076
+ play: "Play now",
1077
+ queue: "Add to the queue (tracks and episodes)"
1078
+ }),
1079
+ shuffle: setting.boolean("Shuffle", { default: false, visibleIf: { mode: "play" } }),
1080
+ text: setting.text("Text", { placeholder: "Shown under the icon", appearance: true })
1061
1081
  },
1062
1082
  run: async ({ settings }) => {
1063
1083
  const target = spotifyUri(settings.item);
@@ -1077,7 +1097,7 @@ var play = defineAction({
1077
1097
  },
1078
1098
  look: (s) => key(s.mode === "queue" ? "lucide/list-end" : "lucide/list-music", s.text)
1079
1099
  });
1080
- var seek = defineAction({
1100
+ var seek = plugin.action({
1081
1101
  id: "seek",
1082
1102
  name: "Seek",
1083
1103
  description: "Jumps forward or back in the track.",
@@ -1085,24 +1105,17 @@ var seek = defineAction({
1085
1105
  icon: "fast-forward",
1086
1106
  slots: ["key", "sensor"],
1087
1107
  settings: {
1088
- direction: {
1089
- type: "select",
1090
- label: "Direction",
1091
- default: "forward",
1092
- options: [
1093
- { value: "forward", label: "Forward" },
1094
- { value: "back", label: "Back" },
1095
- { value: "start", label: "To the start" }
1096
- ]
1097
- },
1098
- seconds: {
1099
- type: "number",
1100
- label: "Seconds",
1108
+ direction: setting.select("Direction", {
1109
+ forward: "Forward",
1110
+ back: "Back",
1111
+ start: "To the start"
1112
+ }),
1113
+ seconds: setting.number("Seconds", {
1101
1114
  default: 15,
1102
1115
  min: 1,
1103
1116
  max: 600,
1104
1117
  visibleIf: { direction: ["forward", "back"] }
1105
- }
1118
+ })
1106
1119
  },
1107
1120
  run: async ({ settings }) => {
1108
1121
  const { positionMs, durationMs } = await current();
@@ -1116,7 +1129,7 @@ var seek = defineAction({
1116
1129
  look: (s) => key(s.direction === "start" ? "lucide/undo-2" : s.direction === "forward" ? "lucide/fast-forward" : "lucide/rewind", s.direction === "start" ? "" : `${s.direction === "forward" ? "+" : "\u2212"}${s.seconds}s`)
1117
1130
  });
1118
1131
  var transfer = (id) => command("PUT", "/me/player", {}, { device_ids: [id], play: true });
1119
- var device = defineAction({
1132
+ var device = plugin.action({
1120
1133
  id: "device",
1121
1134
  name: "Play on a device",
1122
1135
  description: "Moves playback to another speaker, phone or computer.",
@@ -1124,18 +1137,13 @@ var device = defineAction({
1124
1137
  icon: "speaker",
1125
1138
  slots: ["key", "sensor"],
1126
1139
  settings: {
1127
- device: {
1128
- type: "select",
1129
- label: "Device",
1130
- options: [],
1131
- source: "devices",
1140
+ device: setting.pick("Device", sources.devices, {
1141
+ required: true,
1132
1142
  description: "Only devices with Spotify open are listed."
1133
- },
1134
- text: { type: "text", label: "Text", placeholder: "Shown under the icon", appearance: true }
1143
+ }),
1144
+ text: setting.text("Text", { placeholder: "Shown under the icon", appearance: true })
1135
1145
  },
1136
1146
  run: async ({ settings }) => {
1137
- if (!settings.device)
1138
- throw new Error("Pick a device in the key's settings");
1139
1147
  const before = (await readPlayer()).deviceId;
1140
1148
  await transfer(settings.device);
1141
1149
  return {
@@ -1145,7 +1153,7 @@ var device = defineAction({
1145
1153
  },
1146
1154
  look: (s) => key("lucide/speaker", s.text)
1147
1155
  });
1148
- var trackChanged = defineTrigger({
1156
+ var trackChanged = plugin.trigger({
1149
1157
  id: "track-changed",
1150
1158
  name: "When the track changes",
1151
1159
  description: "Fires with the new track whenever something else starts playing.",
@@ -1160,35 +1168,34 @@ var trackChanged = defineTrigger({
1160
1168
  });
1161
1169
  }
1162
1170
  });
1163
- var stateOf = (id, name, type) => ({
1164
- id,
1165
- name,
1166
- type,
1167
- start: (set) => player.listen((p) => set(p?.[id]))
1171
+ var signIn = plugin.command({
1172
+ id: "sign-in",
1173
+ name: "Sign in",
1174
+ description: "Opens Spotify in your browser to allow access.",
1175
+ run: (context) => {
1176
+ signingIn?.cancel();
1177
+ signingIn = spotify.signIn(context.settings().port);
1178
+ context.status("Waiting for you to allow access in the browser", "info");
1179
+ signingIn.done.then(() => {
1180
+ context.status("Signed in to Spotify", "ok");
1181
+ me = undefined;
1182
+ pollNow();
1183
+ }, (error) => context.status(error.message, "error"));
1184
+ return signingIn.url;
1185
+ }
1168
1186
  });
1169
- var playlistOptions = async (writable) => (await playlists()).filter((p) => !writable || p.writable).map((p) => ({ value: writable ? p.id : p.uri, label: p.name }));
1170
- var src_default = definePlugin({
1171
- id: "@sdeck/plugin-spotify",
1172
- name: "Spotify",
1173
- version: "0.3.0",
1174
- namespace: "spotify",
1175
- description: "Now playing, playback, Liked Songs, playlists and devices through the Spotify Web API.",
1176
- author: "sdeck",
1177
- permissions: ["network"],
1178
- settings: {
1179
- clientId: {
1180
- type: "text",
1181
- label: "Client ID",
1182
- description: "Create an app at developer.spotify.com and add the redirect URI http://127.0.0.1:43821/callback (with your port)."
1183
- },
1184
- port: {
1185
- type: "number",
1186
- label: "Sign-in port",
1187
- default: DEFAULT_PORT,
1188
- min: 1024,
1189
- max: 65535
1190
- }
1191
- },
1187
+ var signOut = plugin.command({
1188
+ id: "sign-out",
1189
+ name: "Sign out",
1190
+ run: async (context) => {
1191
+ signingIn?.cancel();
1192
+ await spotify.signOut();
1193
+ me = undefined;
1194
+ context.status("Signed out", "info");
1195
+ pollNow();
1196
+ }
1197
+ });
1198
+ var src_default = plugin.define({
1192
1199
  onboarding: {
1193
1200
  intro: "Spotify lets apps in only with an app of your own. It takes a minute.",
1194
1201
  steps: [
@@ -1225,35 +1232,7 @@ var src_default = definePlugin({
1225
1232
  window: { action: "now-playing" }
1226
1233
  }
1227
1234
  },
1228
- commands: [
1229
- {
1230
- id: "sign-in",
1231
- name: "Sign in",
1232
- description: "Opens Spotify in your browser to allow access.",
1233
- run: (activated) => {
1234
- signingIn?.cancel();
1235
- signingIn = spotify.signIn(Number(activated.settings().port));
1236
- activated.status("Waiting for you to allow access in the browser", "info");
1237
- signingIn.done.then(() => {
1238
- activated.status("Signed in to Spotify", "ok");
1239
- me = undefined;
1240
- pollNow();
1241
- }, (error) => activated.status(error.message, "error"));
1242
- return signingIn.url;
1243
- }
1244
- },
1245
- {
1246
- id: "sign-out",
1247
- name: "Sign out",
1248
- run: async (activated) => {
1249
- signingIn?.cancel();
1250
- await spotify.signOut();
1251
- me = undefined;
1252
- activated.status("Signed out", "info");
1253
- pollNow();
1254
- }
1255
- }
1256
- ],
1235
+ commands: [signIn, signOut],
1257
1236
  actions: [
1258
1237
  playPause,
1259
1238
  next,
@@ -1269,46 +1248,12 @@ var src_default = definePlugin({
1269
1248
  device
1270
1249
  ],
1271
1250
  triggers: [trackChanged],
1272
- optionSources: {
1273
- playlists: () => playlistOptions(true),
1274
- library: () => playlistOptions(false),
1275
- devices: async () => parseDevices(await spotify.request("GET", "/me/player/devices")).map((d) => ({
1276
- value: d.id,
1277
- label: `${d.name}${d.active ? " (playing)" : ""}`
1278
- }))
1279
- },
1280
- state: [
1281
- stateOf("playing", "Playing", "boolean"),
1282
- stateOf("track", "Track", "string"),
1283
- stateOf("artist", "Artist", "string"),
1284
- stateOf("album", "Album", "string"),
1285
- stateOf("art", "Cover art", "string"),
1286
- stateOf("progress", "Progress", "number"),
1287
- { ...stateOf("position", "Position", "number"), unit: "s" },
1288
- { ...stateOf("duration", "Duration", "number"), unit: "s" },
1289
- {
1290
- ...stateOf("startedAt", "Started at", "number"),
1291
- description: "When the track would have started to be where it is now, in ms since 1970. Only while playing: now() - spotify.startedAt is the exact position."
1292
- },
1293
- stateOf("volume", "Volume", "number"),
1294
- stateOf("shuffle", "Shuffle", "boolean"),
1295
- stateOf("repeat", "Repeat", "string"),
1296
- stateOf("device", "Device", "string"),
1297
- stateOf("uri", "Track URI", "string"),
1298
- {
1299
- id: "liked",
1300
- name: "Liked",
1301
- type: "boolean",
1302
- description: "Whether what is playing is in Liked Songs.",
1303
- start: (set) => liked.listen(set)
1304
- }
1305
- ],
1251
+ sources: Object.values(sources),
1252
+ state: Object.values(values),
1306
1253
  activate(activated) {
1307
- context = activated;
1308
1254
  activated.status(activated.secrets.get("refreshToken") ? "Signed in to Spotify" : "Set your client id, then sign in", "info");
1309
1255
  return () => {
1310
1256
  signingIn?.cancel();
1311
- context = undefined;
1312
1257
  };
1313
1258
  }
1314
1259
  });
@@ -1330,4 +1275,4 @@ export {
1330
1275
  spotifyUri
1331
1276
  };
1332
1277
 
1333
- //# debugId=B1A1A70AF5BDAAD764756E2164756E21
1278
+ //# debugId=54804B6B9E93CC9B64756E2164756E21