@flighthq/clipboard 0.1.0 → 0.2.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/package.json CHANGED
@@ -1,6 +1,11 @@
1
1
  {
2
2
  "name": "@flighthq/clipboard",
3
- "version": "0.1.0",
3
+ "version": "0.2.0",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/flighthq/flight.git",
7
+ "directory": "packages/clipboard"
8
+ },
4
9
  "type": "module",
5
10
  "main": "dist/index.js",
6
11
  "types": "dist/index.d.ts",
@@ -27,8 +32,8 @@
27
32
  "clean:dist": "tsx ../../scripts/clean-package-dist.ts"
28
33
  },
29
34
  "dependencies": {
30
- "@flighthq/signals": "0.1.0",
31
- "@flighthq/types": "0.1.0"
35
+ "@flighthq/signals": "0.2.0",
36
+ "@flighthq/types": "0.2.0"
32
37
  },
33
38
  "devDependencies": {
34
39
  "typescript": "^5.3.0"
@@ -1,4 +1,11 @@
1
1
  import type { ClipboardBackend, ClipboardBookmark } from '@flighthq/types';
2
+ import {
3
+ ClipboardFormatBookmark,
4
+ ClipboardFormatHtml,
5
+ ClipboardFormatImage,
6
+ ClipboardFormatRtf,
7
+ ClipboardFormatText,
8
+ } from '@flighthq/types';
2
9
 
3
10
  import {
4
11
  attachClipboardWatch,
@@ -55,15 +62,15 @@ function fakeBackend(): ClipboardBackend & {
55
62
  formats: {},
56
63
  changeCount: 0,
57
64
  async readFormat(format) {
58
- if (format === 'text/plain') return this.text;
59
- if (format === 'text/html') return this.html;
60
- if (format === 'text/rtf') return this.rtf;
65
+ if (format === ClipboardFormatText) return this.text;
66
+ if (format === ClipboardFormatHtml) return this.html;
67
+ if (format === ClipboardFormatRtf) return this.rtf;
61
68
  return this.formats[format] ?? '';
62
69
  },
63
70
  async writeFormat(format, data) {
64
- if (format === 'text/plain') this.text = data;
65
- else if (format === 'text/html') this.html = data;
66
- else if (format === 'text/rtf') this.rtf = data;
71
+ if (format === ClipboardFormatText) this.text = data;
72
+ else if (format === ClipboardFormatHtml) this.html = data;
73
+ else if (format === ClipboardFormatRtf) this.rtf = data;
67
74
  else this.formats[format] = data;
68
75
  this.changeCount++;
69
76
  return true;
@@ -74,11 +81,11 @@ function fakeBackend(): ClipboardBackend & {
74
81
  },
75
82
  async getFormats() {
76
83
  const out: string[] = [];
77
- if (this.text.length > 0) out.push('text/plain');
78
- if (this.html.length > 0) out.push('text/html');
79
- if (this.rtf.length > 0) out.push('text/rtf');
80
- if (this.image.length > 0) out.push('image/png');
81
- if (this.bookmark !== null) out.push('text/x-moz-url');
84
+ if (this.text.length > 0) out.push(ClipboardFormatText);
85
+ if (this.html.length > 0) out.push(ClipboardFormatHtml);
86
+ if (this.rtf.length > 0) out.push(ClipboardFormatRtf);
87
+ if (this.image.length > 0) out.push(ClipboardFormatImage);
88
+ if (this.bookmark !== null) out.push(ClipboardFormatBookmark);
82
89
  for (const k of Object.keys(this.formats)) out.push(k);
83
90
  return out;
84
91
  },
@@ -228,7 +235,7 @@ describe('createWebClipboardBackend', () => {
228
235
  const backend = createWebClipboardBackend();
229
236
  expect(typeof (await backend.readText())).toBe('string');
230
237
  expect(typeof (await backend.readHtml())).toBe('string');
231
- expect(typeof (await backend.readFormat('text/plain'))).toBe('string');
238
+ expect(typeof (await backend.readFormat(ClipboardFormatText))).toBe('string');
232
239
  });
233
240
 
234
241
  it('getFormats returns an array without throwing', async () => {
@@ -317,7 +324,7 @@ describe('getClipboardFormats', () => {
317
324
  expect(await getClipboardFormats()).toEqual([]);
318
325
  await writeClipboardText('hi');
319
326
  const formats = await getClipboardFormats();
320
- expect(formats).toContain('text/plain');
327
+ expect(formats).toContain(ClipboardFormatText);
321
328
  });
322
329
 
323
330
  it('returns [] from the web backend without throwing', async () => {
@@ -335,8 +342,8 @@ describe('hasClipboardBookmark', () => {
335
342
 
336
343
  it('returns true after a bookmark is written by the backend', async () => {
337
344
  const backend = fakeBackend();
338
- // Simulate a backend that puts 'text/x-moz-url' in formats when a bookmark is written
339
- backend.formats['text/x-moz-url'] = 'https://example.com\nFlight';
345
+ // Simulate a backend that puts ClipboardFormatBookmark in formats when a bookmark is written
346
+ backend.formats[ClipboardFormatBookmark] = 'https://example.com\nFlight';
340
347
  setClipboardBackend(backend);
341
348
  expect(await hasClipboardBookmark()).toBe(true);
342
349
  });
@@ -346,9 +353,9 @@ describe('hasClipboardFormat', () => {
346
353
  it('reflects whether a format is present', async () => {
347
354
  const backend = fakeBackend();
348
355
  setClipboardBackend(backend);
349
- expect(await hasClipboardFormat('text/plain')).toBe(false);
356
+ expect(await hasClipboardFormat(ClipboardFormatText)).toBe(false);
350
357
  await writeClipboardText('x');
351
- expect(await hasClipboardFormat('text/plain')).toBe(true);
358
+ expect(await hasClipboardFormat(ClipboardFormatText)).toBe(true);
352
359
  });
353
360
  });
354
361
 
@@ -402,22 +409,22 @@ describe('readClipboard', () => {
402
409
  setClipboardBackend(backend);
403
410
  await writeClipboardText('hello');
404
411
  await writeClipboardHtml('<b>hello</b>');
405
- const result = await readClipboard(['text/plain', 'text/html']);
406
- expect(result['text/plain']).toBe('hello');
407
- expect(result['text/html']).toBe('<b>hello</b>');
412
+ const result = await readClipboard([ClipboardFormatText, ClipboardFormatHtml]);
413
+ expect(result[ClipboardFormatText]).toBe('hello');
414
+ expect(result[ClipboardFormatHtml]).toBe('<b>hello</b>');
408
415
  });
409
416
 
410
417
  it('omits formats that are not present', async () => {
411
418
  const backend = fakeBackend();
412
419
  setClipboardBackend(backend);
413
420
  await writeClipboardText('hi');
414
- const result = await readClipboard(['text/plain', 'text/rtf']);
415
- expect(result['text/plain']).toBe('hi');
416
- expect(result['text/rtf']).toBeUndefined();
421
+ const result = await readClipboard([ClipboardFormatText, ClipboardFormatRtf]);
422
+ expect(result[ClipboardFormatText]).toBe('hi');
423
+ expect(result[ClipboardFormatRtf]).toBeUndefined();
417
424
  });
418
425
 
419
426
  it('returns {} from the web backend without throwing', async () => {
420
- const result = await createWebClipboardBackend().readItems(['text/plain']);
427
+ const result = await createWebClipboardBackend().readItems([ClipboardFormatText]);
421
428
  expect(typeof result).toBe('object');
422
429
  });
423
430
  });
@@ -516,8 +523,8 @@ describe('writeClipboard', () => {
516
523
  setClipboardBackend(backend);
517
524
  expect(
518
525
  await writeClipboard([
519
- { format: 'text/plain', data: 'hello' },
520
- { format: 'text/html', data: '<b>hello</b>' },
526
+ { format: ClipboardFormatText, data: 'hello' },
527
+ { format: ClipboardFormatHtml, data: '<b>hello</b>' },
521
528
  ]),
522
529
  ).toBe(true);
523
530
  expect(backend.text).toBe('hello');
@@ -525,7 +532,7 @@ describe('writeClipboard', () => {
525
532
  });
526
533
 
527
534
  it('returns false from the web backend without throwing', async () => {
528
- expect(await createWebClipboardBackend().writeItems([{ format: 'text/plain', data: 'x' }])).toBe(false);
535
+ expect(await createWebClipboardBackend().writeItems([{ format: ClipboardFormatText, data: 'x' }])).toBe(false);
529
536
  });
530
537
  });
531
538