@sdeck/plugin-spotify 0.2.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";
@@ -223,6 +228,9 @@ var IDLE = {
223
228
  art: "",
224
229
  progress: 0,
225
230
  positionMs: 0,
231
+ position: 0,
232
+ duration: 0,
233
+ startedAt: undefined,
226
234
  durationMs: 0,
227
235
  volume: undefined,
228
236
  shuffle: false,
@@ -231,7 +239,7 @@ var IDLE = {
231
239
  deviceId: "",
232
240
  context: ""
233
241
  };
234
- function parsePlayer(body) {
242
+ function parsePlayer(body, at = Date.now()) {
235
243
  if (typeof body !== "object" || body === null)
236
244
  return IDLE;
237
245
  const raw = body;
@@ -249,6 +257,9 @@ function parsePlayer(body) {
249
257
  art,
250
258
  progress: duration > 0 ? Math.round(100 * position / duration) : 0,
251
259
  positionMs: position,
260
+ position: position / 1000,
261
+ duration: duration / 1000,
262
+ startedAt: raw.is_playing && item ? Math.round(at - position) : undefined,
252
263
  durationMs: duration,
253
264
  volume: raw.device?.volume_percent ?? undefined,
254
265
  shuffle: raw.shuffle_state === true,
@@ -299,6 +310,7 @@ function parseDevices(body) {
299
310
 
300
311
  // src/panels.tsx
301
312
  import { useHostState, useImage } from "@sdeck/plugin";
313
+ import { surfaceStyle } from "@sdeck/schema";
302
314
  import { jsx, jsxs } from "react/jsx-runtime";
303
315
  var GREEN = "#1ed760";
304
316
  var BACKGROUND = "#121212";
@@ -350,7 +362,7 @@ function NowPlaying({ kind }) {
350
362
  width: "100%",
351
363
  height: "100%",
352
364
  alignItems: "center",
353
- background: BACKGROUND,
365
+ ...surfaceStyle(BACKGROUND),
354
366
  gap: 4,
355
367
  padding: "0 4px"
356
368
  },
@@ -380,7 +392,7 @@ function NowPlaying({ kind }) {
380
392
  alignItems: "center",
381
393
  justifyContent: "center",
382
394
  gap: 8,
383
- background: BACKGROUND
395
+ ...surfaceStyle(BACKGROUND)
384
396
  },
385
397
  children: [
386
398
  /* @__PURE__ */ jsx(Icon, {
@@ -389,7 +401,7 @@ function NowPlaying({ kind }) {
389
401
  color: GREEN
390
402
  }),
391
403
  /* @__PURE__ */ jsx("div", {
392
- style: { ...box, color: MUTED, fontSize: kind === "window" ? 15 : 11, fontWeight: 700 },
404
+ style: { ...box, color: MUTED, fontSize: kind === "window" ? 15 : 12, fontWeight: 700 },
393
405
  children: "Not playing"
394
406
  })
395
407
  ]
@@ -402,7 +414,7 @@ function NowPlaying({ kind }) {
402
414
  width: "100%",
403
415
  height: "100%",
404
416
  flexDirection: "column",
405
- background: BACKGROUND
417
+ ...surfaceStyle(BACKGROUND)
406
418
  },
407
419
  children: [
408
420
  /* @__PURE__ */ jsxs("div", {
@@ -485,7 +497,7 @@ function NowPlaying({ kind }) {
485
497
  width: "100%",
486
498
  height: "100%",
487
499
  position: "relative",
488
- background: BACKGROUND
500
+ ...surfaceStyle(BACKGROUND)
489
501
  },
490
502
  children: [
491
503
  art ? /* @__PURE__ */ jsx("img", {
@@ -564,15 +576,31 @@ function NowPlaying({ kind }) {
564
576
  // src/index.tsx
565
577
  import { jsx as jsx2 } from "react/jsx-runtime";
566
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
+ });
567
596
  var POLL_MS = 3000;
568
- var context;
569
597
  var signingIn;
570
598
  var spotify = new SpotifyClient({
571
- clientId: () => String(context?.settings().clientId ?? "").trim(),
599
+ clientId: () => plugin.context?.settings().clientId.trim() ?? "",
572
600
  tokens: {
573
- get: () => context?.secrets.get("refreshToken"),
601
+ get: () => plugin.context?.secrets.get("refreshToken"),
574
602
  set: async (token) => {
575
- await context?.secrets.set("refreshToken", token);
603
+ await plugin.context?.secrets.set("refreshToken", token);
576
604
  }
577
605
  }
578
606
  });
@@ -588,10 +616,15 @@ var player = new Shared((emit) => {
588
616
  clearTimeout(timer);
589
617
  let wait = POLL_MS;
590
618
  try {
591
- const next = parsePlayer(await spotify.request("GET", "/me/player"));
619
+ const asked = Date.now();
620
+ const body = await spotify.request("GET", "/me/player");
621
+ const next = parsePlayer(body, (asked + Date.now()) / 2);
592
622
  if (!stopped)
593
623
  emit(next);
594
- context?.status("Signed in to Spotify", "ok");
624
+ const left = next.durationMs - next.positionMs;
625
+ if (next.playing && left > 0)
626
+ wait = Math.min(wait, left + 400);
627
+ plugin.context?.status("Signed in to Spotify", "ok");
595
628
  } catch (error) {
596
629
  if (!stopped)
597
630
  emit(undefined);
@@ -599,7 +632,7 @@ var player = new Shared((emit) => {
599
632
  wait = error.retryAfterMs;
600
633
  else if (error instanceof SignedOut)
601
634
  wait = 1e4;
602
- context?.status(error.message, error instanceof SignedOut ? "info" : "error");
635
+ plugin.context?.status(error.message, error instanceof SignedOut ? "info" : "error");
603
636
  }
604
637
  running = false;
605
638
  if (!stopped)
@@ -687,14 +720,53 @@ async function playlists() {
687
720
  }
688
721
  return all;
689
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
+ };
690
762
  var key = (icon, caption = "", color = "#ffffff") => iconLook({ icon, caption, background: BACKGROUND, color });
691
763
  var dot = (active) => iconLayer("dot", disc(GREEN), { size: 7, position: "bottom", visible: active });
692
764
  var switched = (active, on, off, colors) => [
693
- iconLayer("on", on, { color: colors.on, size: 58, visible: active }),
694
- 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})` })
695
767
  ];
696
768
  var setPlaying = (play) => command("PUT", play ? "/me/player/play" : "/me/player/pause");
697
- var playPause = defineAction({
769
+ var playPause = plugin.action({
698
770
  id: "play-pause",
699
771
  name: "Play / pause",
700
772
  description: "Shows the cover of what is playing; pressing it plays or pauses.",
@@ -702,7 +774,7 @@ var playPause = defineAction({
702
774
  icon: "play",
703
775
  slots: ["key", "sensor"],
704
776
  settings: {
705
- cover: { type: "boolean", label: "Show the cover art", default: true, appearance: true }
777
+ cover: setting.boolean("Show the cover art", { default: true, appearance: true })
706
778
  },
707
779
  run: async () => {
708
780
  const before = (await readPlayer()).playing;
@@ -719,34 +791,37 @@ var playPause = defineAction({
719
791
  iconLayer("pause", "tb/TbPlayerPauseFilled", {
720
792
  color: "#000000",
721
793
  frame: centered(34),
722
- visible: `(${visible}) && spotify.playing`
794
+ visible: `(${visible}) && ${values.playing.key}`
723
795
  }),
724
796
  iconLayer("play", "tb/TbPlayerPlayFilled", {
725
797
  color: "#000000",
726
798
  frame: { ...centered(34), x: 35 },
727
- visible: `(${visible}) && !spotify.playing`
799
+ visible: `(${visible}) && !${values.playing.key}`
728
800
  })
729
801
  ];
730
802
  if (!s.cover)
731
- return { background: BACKGROUND, layers: button("true") };
803
+ return { background: surface(BACKGROUND), layers: button("true") };
732
804
  return {
733
- background: BACKGROUND,
805
+ background: surface(BACKGROUND),
734
806
  layers: [
735
- 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
+ }),
736
811
  iconLayer("shade", disc("#000000", 0.6), {
737
812
  frame: centered(48),
738
- visible: "spotify.art && !spotify.playing"
813
+ visible: `${values.art.key} && !${values.playing.key}`
739
814
  }),
740
815
  iconLayer("resume", "tb/TbPlayerPlayFilled", {
741
816
  frame: { ...centered(24), x: 39 },
742
- visible: "spotify.art && !spotify.playing"
817
+ visible: `${values.art.key} && !${values.playing.key}`
743
818
  }),
744
- ...button("!spotify.art")
819
+ ...button(`!${values.art.key}`)
745
820
  ]
746
821
  };
747
822
  }
748
823
  });
749
- var skip = (id, name, icon, back) => defineAction({
824
+ var skip = (id, name, icon, back) => plugin.action({
750
825
  id,
751
826
  name,
752
827
  category: "Spotify",
@@ -760,7 +835,7 @@ var skip = (id, name, icon, back) => defineAction({
760
835
  });
761
836
  var next = skip("next", "Next track", "skip-forward", "previous");
762
837
  var previous = skip("previous", "Previous track", "skip-back", "next");
763
- var nowPlaying = defineAction({
838
+ var nowPlaying = plugin.action({
764
839
  id: "now-playing",
765
840
  name: "Now playing",
766
841
  description: "The current track and artist, with how far through it is.",
@@ -768,17 +843,12 @@ var nowPlaying = defineAction({
768
843
  icon: "music",
769
844
  slots: ["window", "key", "sensor"],
770
845
  settings: {
771
- press: {
772
- type: "select",
773
- label: "When pressed",
774
- default: "play-pause",
775
- options: [
776
- { value: "play-pause", label: "Play or pause" },
777
- { value: "next", label: "Next track" },
778
- { value: "like", label: "Like or unlike" },
779
- { value: "none", label: "Nothing" }
780
- ]
781
- }
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
+ })
782
852
  },
783
853
  display: true,
784
854
  run: async () => ({ result: await readPlayer() }),
@@ -790,52 +860,38 @@ var nowPlaying = defineAction({
790
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));
791
861
  done.catch((error) => plugin.log("now playing:", error.message));
792
862
  };
793
- useKeyPress(slot.kind === "key" ? act : undefined, {
794
- key: slot.kind === "key" ? slot.index : -1
795
- });
796
- useSensorPress(slot.kind === "sensor" ? act : undefined, {
797
- sensor: slot.kind === "sensor" ? slot.index : -1
798
- });
863
+ usePress(act);
799
864
  return /* @__PURE__ */ jsx2(NowPlaying, {
800
865
  kind: slot.kind
801
866
  });
802
867
  }
803
868
  });
804
869
  var unmuted = 50;
805
- var volume = defineAction({
870
+ var volume = plugin.action({
806
871
  id: "volume",
807
872
  name: "Spotify volume",
808
873
  category: "Spotify",
809
874
  icon: "volume-1",
810
875
  slots: ["key", "sensor"],
811
876
  settings: {
812
- operation: {
813
- type: "select",
814
- label: "When pressed",
815
- default: "up",
816
- options: [
817
- { value: "up", label: "Volume up" },
818
- { value: "down", label: "Volume down" },
819
- { value: "set", label: "Set a level" },
820
- { value: "mute", label: "Mute / unmute" }
821
- ]
822
- },
823
- step: {
824
- type: "number",
825
- 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 (%)", {
826
884
  default: 10,
827
885
  min: 1,
828
886
  max: 50,
829
887
  visibleIf: { operation: ["up", "down"] }
830
- },
831
- level: {
832
- type: "number",
833
- label: "Level (%)",
888
+ }),
889
+ level: setting.number("Level (%)", {
834
890
  default: 50,
835
891
  min: 0,
836
892
  max: 100,
837
893
  visibleIf: { operation: "set" }
838
- }
894
+ })
839
895
  },
840
896
  run: async ({ settings }) => {
841
897
  const before = (await readPlayer()).volume;
@@ -850,44 +906,38 @@ var volume = defineAction({
850
906
  await set(level);
851
907
  return { result: level, label: `Spotify volume ${level}%`, undo: () => set(before) };
852
908
  },
853
- look: (s) => {
854
- const icon = s.operation === "mute" ? "lucide/volume-x" : s.operation === "down" ? "lucide/volume-1" : "lucide/volume-2";
855
- const level = {
856
- id: "level",
857
- type: "bar",
858
- value: "spotify.volume ?? 0",
859
- color: GREEN,
860
- track: FAINT,
861
- position: "bottom",
862
- thickness: 6
863
- };
864
- if (s.operation === "mute") {
865
- return {
866
- background: BACKGROUND,
867
- layers: [
868
- ...switched("spotify.volume == 0", icon, "lucide/volume-2", {
869
- on: "#f87171",
870
- off: "#ffffff"
871
- }),
872
- level
873
- ]
874
- };
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
+ }
875
937
  }
876
- return {
877
- background: BACKGROUND,
878
- layers: [
879
- iconLayer("icon", icon, {
880
- color: "#ffffff",
881
- size: s.operation === "set" ? 42 : 52,
882
- position: s.operation === "set" ? "top" : "middle"
883
- }),
884
- ...s.operation === "set" ? [textLayer("label", `${s.level}%`, { color: MUTED, size: 14, position: "bottom" })] : [],
885
- level
886
- ]
887
- };
888
938
  }
889
939
  });
890
- var shuffle = defineAction({
940
+ var shuffle = plugin.action({
891
941
  id: "shuffle",
892
942
  name: "Shuffle",
893
943
  category: "Spotify",
@@ -900,20 +950,21 @@ var shuffle = defineAction({
900
950
  return { result: !before, label: `Shuffle ${before ? "off" : "on"}`, undo: () => set(before) };
901
951
  },
902
952
  look: () => ({
903
- background: BACKGROUND,
953
+ background: surface(BACKGROUND),
904
954
  layers: [
905
- ...switched("spotify.shuffle", "lucide/shuffle", "lucide/shuffle", {
955
+ ...switched(values.shuffle.key, "lucide/shuffle", "lucide/shuffle", {
906
956
  on: GREEN,
907
957
  off: MUTED
908
958
  }),
909
- dot("spotify.shuffle")
959
+ dot(values.shuffle.key)
910
960
  ]
911
961
  })
912
962
  });
913
963
  function nextRepeat(mode) {
914
964
  return mode === "off" ? "context" : mode === "context" ? "track" : "off";
915
965
  }
916
- var repeat = defineAction({
966
+ var repeating = `${values.repeat.key} == "context" || ${values.repeat.key} == "track"`;
967
+ var repeat = plugin.action({
917
968
  id: "repeat",
918
969
  name: "Repeat",
919
970
  description: "Cycles repeat: off, the playlist, one track.",
@@ -928,28 +979,28 @@ var repeat = defineAction({
928
979
  return { result: state, label: `Repeat ${state}`, undo: () => set(before) };
929
980
  },
930
981
  look: () => ({
931
- background: BACKGROUND,
982
+ background: surface(BACKGROUND),
932
983
  layers: [
933
984
  iconLayer("off", "lucide/repeat", {
934
985
  color: MUTED,
935
- size: 58,
936
- visible: 'spotify.repeat != "context" && spotify.repeat != "track"'
986
+ size: iconSize(false),
987
+ visible: `!(${repeating})`
937
988
  }),
938
989
  iconLayer("all", "lucide/repeat", {
939
990
  color: GREEN,
940
- size: 58,
941
- visible: 'spotify.repeat == "context"'
991
+ size: iconSize(false),
992
+ visible: `${values.repeat.key} == "context"`
942
993
  }),
943
994
  iconLayer("one", "lucide/repeat-1", {
944
995
  color: GREEN,
945
- size: 58,
946
- visible: 'spotify.repeat == "track"'
996
+ size: iconSize(false),
997
+ visible: `${values.repeat.key} == "track"`
947
998
  }),
948
- dot('spotify.repeat == "context" || spotify.repeat == "track"')
999
+ dot(repeating)
949
1000
  ]
950
1001
  })
951
1002
  });
952
- var like = defineAction({
1003
+ var like = plugin.action({
953
1004
  id: "like",
954
1005
  name: "Like",
955
1006
  description: "Adds what is playing to Liked Songs, or takes it out. Lit while it is liked.",
@@ -957,16 +1008,11 @@ var like = defineAction({
957
1008
  icon: "heart",
958
1009
  slots: ["key", "sensor"],
959
1010
  settings: {
960
- mode: {
961
- type: "select",
962
- label: "When pressed",
963
- default: "toggle",
964
- options: [
965
- { value: "toggle", label: "Like / unlike" },
966
- { value: "like", label: "Like" },
967
- { value: "unlike", label: "Unlike" }
968
- ]
969
- }
1011
+ mode: setting.select("When pressed", {
1012
+ toggle: "Like / unlike",
1013
+ like: "Like",
1014
+ unlike: "Unlike"
1015
+ })
970
1016
  },
971
1017
  run: async ({ settings }) => {
972
1018
  const { uri, track } = await current();
@@ -981,14 +1027,14 @@ var like = defineAction({
981
1027
  };
982
1028
  },
983
1029
  look: () => ({
984
- background: BACKGROUND,
985
- layers: switched("spotify.liked == true", "tb/TbHeartFilled", "tb/TbHeart", {
1030
+ background: surface(BACKGROUND),
1031
+ layers: switched(`${values.liked.key} == true`, "tb/TbHeartFilled", "tb/TbHeart", {
986
1032
  on: GREEN,
987
1033
  off: MUTED
988
1034
  })
989
1035
  })
990
1036
  });
991
- var addToPlaylist = defineAction({
1037
+ var addToPlaylist = plugin.action({
992
1038
  id: "add-to-playlist",
993
1039
  name: "Add to playlist",
994
1040
  description: "Adds what is playing to one of your playlists.",
@@ -996,18 +1042,13 @@ var addToPlaylist = defineAction({
996
1042
  icon: "list-plus",
997
1043
  slots: ["key", "sensor"],
998
1044
  settings: {
999
- playlist: {
1000
- type: "select",
1001
- label: "Playlist",
1002
- options: [],
1003
- source: "playlists",
1045
+ playlist: setting.pick("Playlist", sources.playlists, {
1046
+ required: true,
1004
1047
  description: "Yours, and ones you collaborate on."
1005
- },
1006
- 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 })
1007
1050
  },
1008
1051
  run: async ({ settings }) => {
1009
- if (!settings.playlist)
1010
- throw new Error("Pick a playlist in the key's settings");
1011
1052
  const { uri, track } = await current();
1012
1053
  await spotify.request("POST", `/playlists/${encodeURIComponent(settings.playlist)}/items`, {}, {
1013
1054
  uris: [uri]
@@ -1016,7 +1057,7 @@ var addToPlaylist = defineAction({
1016
1057
  },
1017
1058
  look: (s) => key("lucide/list-plus", s.text)
1018
1059
  });
1019
- var play = defineAction({
1060
+ var play = plugin.action({
1020
1061
  id: "play",
1021
1062
  name: "Play",
1022
1063
  description: "Plays a playlist, album, artist, track or episode, or adds it to the queue.",
@@ -1024,29 +1065,19 @@ var play = defineAction({
1024
1065
  icon: "list-music",
1025
1066
  slots: ["key", "sensor"],
1026
1067
  settings: {
1027
- item: {
1028
- type: "text",
1029
- label: "What to play",
1030
- 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",
1031
1072
  placeholder: "A Spotify link, or pick a playlist",
1032
1073
  description: "In Spotify: Share \u2192 Copy link."
1033
- },
1034
- mode: {
1035
- type: "select",
1036
- label: "When pressed",
1037
- default: "play",
1038
- options: [
1039
- { value: "play", label: "Play now" },
1040
- { value: "queue", label: "Add to the queue (tracks and episodes)" }
1041
- ]
1042
- },
1043
- shuffle: {
1044
- type: "boolean",
1045
- label: "Shuffle",
1046
- default: false,
1047
- visibleIf: { mode: "play" }
1048
- },
1049
- 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 })
1050
1081
  },
1051
1082
  run: async ({ settings }) => {
1052
1083
  const target = spotifyUri(settings.item);
@@ -1066,7 +1097,7 @@ var play = defineAction({
1066
1097
  },
1067
1098
  look: (s) => key(s.mode === "queue" ? "lucide/list-end" : "lucide/list-music", s.text)
1068
1099
  });
1069
- var seek = defineAction({
1100
+ var seek = plugin.action({
1070
1101
  id: "seek",
1071
1102
  name: "Seek",
1072
1103
  description: "Jumps forward or back in the track.",
@@ -1074,24 +1105,17 @@ var seek = defineAction({
1074
1105
  icon: "fast-forward",
1075
1106
  slots: ["key", "sensor"],
1076
1107
  settings: {
1077
- direction: {
1078
- type: "select",
1079
- label: "Direction",
1080
- default: "forward",
1081
- options: [
1082
- { value: "forward", label: "Forward" },
1083
- { value: "back", label: "Back" },
1084
- { value: "start", label: "To the start" }
1085
- ]
1086
- },
1087
- seconds: {
1088
- type: "number",
1089
- label: "Seconds",
1108
+ direction: setting.select("Direction", {
1109
+ forward: "Forward",
1110
+ back: "Back",
1111
+ start: "To the start"
1112
+ }),
1113
+ seconds: setting.number("Seconds", {
1090
1114
  default: 15,
1091
1115
  min: 1,
1092
1116
  max: 600,
1093
1117
  visibleIf: { direction: ["forward", "back"] }
1094
- }
1118
+ })
1095
1119
  },
1096
1120
  run: async ({ settings }) => {
1097
1121
  const { positionMs, durationMs } = await current();
@@ -1105,7 +1129,7 @@ var seek = defineAction({
1105
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`)
1106
1130
  });
1107
1131
  var transfer = (id) => command("PUT", "/me/player", {}, { device_ids: [id], play: true });
1108
- var device = defineAction({
1132
+ var device = plugin.action({
1109
1133
  id: "device",
1110
1134
  name: "Play on a device",
1111
1135
  description: "Moves playback to another speaker, phone or computer.",
@@ -1113,18 +1137,13 @@ var device = defineAction({
1113
1137
  icon: "speaker",
1114
1138
  slots: ["key", "sensor"],
1115
1139
  settings: {
1116
- device: {
1117
- type: "select",
1118
- label: "Device",
1119
- options: [],
1120
- source: "devices",
1140
+ device: setting.pick("Device", sources.devices, {
1141
+ required: true,
1121
1142
  description: "Only devices with Spotify open are listed."
1122
- },
1123
- 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 })
1124
1145
  },
1125
1146
  run: async ({ settings }) => {
1126
- if (!settings.device)
1127
- throw new Error("Pick a device in the key's settings");
1128
1147
  const before = (await readPlayer()).deviceId;
1129
1148
  await transfer(settings.device);
1130
1149
  return {
@@ -1134,7 +1153,7 @@ var device = defineAction({
1134
1153
  },
1135
1154
  look: (s) => key("lucide/speaker", s.text)
1136
1155
  });
1137
- var trackChanged = defineTrigger({
1156
+ var trackChanged = plugin.trigger({
1138
1157
  id: "track-changed",
1139
1158
  name: "When the track changes",
1140
1159
  description: "Fires with the new track whenever something else starts playing.",
@@ -1149,35 +1168,34 @@ var trackChanged = defineTrigger({
1149
1168
  });
1150
1169
  }
1151
1170
  });
1152
- var stateOf = (id, name, type) => ({
1153
- id,
1154
- name,
1155
- type,
1156
- 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
+ }
1157
1186
  });
1158
- var playlistOptions = async (writable) => (await playlists()).filter((p) => !writable || p.writable).map((p) => ({ value: writable ? p.id : p.uri, label: p.name }));
1159
- var src_default = definePlugin({
1160
- id: "@sdeck/plugin-spotify",
1161
- name: "Spotify",
1162
- version: "0.2.0",
1163
- namespace: "spotify",
1164
- description: "Now playing, playback, Liked Songs, playlists and devices through the Spotify Web API.",
1165
- author: "sdeck",
1166
- permissions: ["network"],
1167
- settings: {
1168
- clientId: {
1169
- type: "text",
1170
- label: "Client ID",
1171
- description: "Create an app at developer.spotify.com and add the redirect URI http://127.0.0.1:43821/callback (with your port)."
1172
- },
1173
- port: {
1174
- type: "number",
1175
- label: "Sign-in port",
1176
- default: DEFAULT_PORT,
1177
- min: 1024,
1178
- max: 65535
1179
- }
1180
- },
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({
1181
1199
  onboarding: {
1182
1200
  intro: "Spotify lets apps in only with an app of your own. It takes a minute.",
1183
1201
  steps: [
@@ -1214,35 +1232,7 @@ var src_default = definePlugin({
1214
1232
  window: { action: "now-playing" }
1215
1233
  }
1216
1234
  },
1217
- commands: [
1218
- {
1219
- id: "sign-in",
1220
- name: "Sign in",
1221
- description: "Opens Spotify in your browser to allow access.",
1222
- run: (activated) => {
1223
- signingIn?.cancel();
1224
- signingIn = spotify.signIn(Number(activated.settings().port));
1225
- activated.status("Waiting for you to allow access in the browser", "info");
1226
- signingIn.done.then(() => {
1227
- activated.status("Signed in to Spotify", "ok");
1228
- me = undefined;
1229
- pollNow();
1230
- }, (error) => activated.status(error.message, "error"));
1231
- return signingIn.url;
1232
- }
1233
- },
1234
- {
1235
- id: "sign-out",
1236
- name: "Sign out",
1237
- run: async (activated) => {
1238
- signingIn?.cancel();
1239
- await spotify.signOut();
1240
- me = undefined;
1241
- activated.status("Signed out", "info");
1242
- pollNow();
1243
- }
1244
- }
1245
- ],
1235
+ commands: [signIn, signOut],
1246
1236
  actions: [
1247
1237
  playPause,
1248
1238
  next,
@@ -1258,40 +1248,12 @@ var src_default = definePlugin({
1258
1248
  device
1259
1249
  ],
1260
1250
  triggers: [trackChanged],
1261
- optionSources: {
1262
- playlists: () => playlistOptions(true),
1263
- library: () => playlistOptions(false),
1264
- devices: async () => parseDevices(await spotify.request("GET", "/me/player/devices")).map((d) => ({
1265
- value: d.id,
1266
- label: `${d.name}${d.active ? " (playing)" : ""}`
1267
- }))
1268
- },
1269
- state: [
1270
- stateOf("playing", "Playing", "boolean"),
1271
- stateOf("track", "Track", "string"),
1272
- stateOf("artist", "Artist", "string"),
1273
- stateOf("album", "Album", "string"),
1274
- stateOf("art", "Cover art", "string"),
1275
- stateOf("progress", "Progress", "number"),
1276
- stateOf("volume", "Volume", "number"),
1277
- stateOf("shuffle", "Shuffle", "boolean"),
1278
- stateOf("repeat", "Repeat", "string"),
1279
- stateOf("device", "Device", "string"),
1280
- stateOf("uri", "Track URI", "string"),
1281
- {
1282
- id: "liked",
1283
- name: "Liked",
1284
- type: "boolean",
1285
- description: "Whether what is playing is in Liked Songs.",
1286
- start: (set) => liked.listen(set)
1287
- }
1288
- ],
1251
+ sources: Object.values(sources),
1252
+ state: Object.values(values),
1289
1253
  activate(activated) {
1290
- context = activated;
1291
1254
  activated.status(activated.secrets.get("refreshToken") ? "Signed in to Spotify" : "Set your client id, then sign in", "info");
1292
1255
  return () => {
1293
1256
  signingIn?.cancel();
1294
- context = undefined;
1295
1257
  };
1296
1258
  }
1297
1259
  });
@@ -1313,4 +1275,4 @@ export {
1313
1275
  spotifyUri
1314
1276
  };
1315
1277
 
1316
- //# debugId=A943F62868708C8764756E2164756E21
1278
+ //# debugId=54804B6B9E93CC9B64756E2164756E21