xonecas.ioDon't click! Sean Caetano Martin Sign in
tiny-adventure / web / js / feedback.js 84 lines4.0 kB Raw
1import { drawText, wrap, lineHeight } from './text.js';
2import { T } from './theme.js';
3
4const MAX = 2000;
5const MIN = 3;
6const SIZE = 14;
7
8export function createFeedback(layer, m, f) {
9  const area = document.createElement('textarea');
10  area.maxLength = MAX;
11  area.setAttribute('aria-label', 'Feedback');
12  Object.assign(area.style, { position: 'fixed', left: '0', top: '0', width: '1px', height: '1px', opacity: '0', border: '0', padding: '0', resize: 'none' });
13  document.body.append(area);
14  const focused = () => document.activeElement === area;
15
16  const cta = layer.add('feedback-cta', {
17    z: 2,
18    draw(ctx, w, h, wd) {
19      f.button(ctx, 0, 0, w, h, 'Give feedback?', { hover: wd.hovered('feedback-open'), size: 13 });
20      wd.hit(0, 0, w, h, { name: 'feedback-open', key: 'feedback-open', onClick: m.openFeedback });
21    },
22  });
23
24  const panel = layer.add('panel-feedback', {
25    z: 3,
26    draw(ctx, w, h, wd) {
27      const pad = 16;
28      f.panel(ctx, 0, 0, w, h, 'surface');
29      drawText(ctx, 'Feedback', pad, 12, { h: 30, size: 22, display: true });
30      let y = 46;
31      for (const line of wrap(ctx, 'What worked, what did not? Your note goes in with this game\'s log and nothing else about you.', 13, 'normal', w - pad * 2)) {
32        drawText(ctx, line, pad, y, { h: lineHeight(13), size: 13, color: T.muted });
33        y += lineHeight(13);
34      }
35      y += 8;
36      const bottom = h - pad - 40 - 26;
37      const on = focused();
38      f.panel(ctx, pad, y, w - pad * 2, bottom - y, 'bg', on ? T.accent : T.line);
39      wd.hit(pad, y, w - pad * 2, bottom - y, { name: 'feedback-text', key: 'feedback-text', onClick: () => area.focus() });
40      const text = area.value;
41      const rows = Math.max(1, Math.floor((bottom - y - 16) / lineHeight(SIZE)));
42      if (!text && !on) drawText(ctx, 'Click here and type', pad + 10, y + 8, { h: lineHeight(SIZE), size: SIZE, color: T.faint });
43      else {
44        const lines = wrap(ctx, text + (on ? '|' : ''), SIZE, 'normal', w - pad * 2 - 20);
45        lines.slice(-rows).forEach((l, i) => drawText(ctx, l, pad + 10, y + 8 + i * lineHeight(SIZE), { h: lineHeight(SIZE), size: SIZE, maxW: w - pad * 2 - 20, fit: true }));
46      }
47      const status = m.feedbackError || (on ? 'Ctrl+Enter sends, Esc closes' : '');
48      drawText(ctx, status, pad, bottom + 4, { h: 18, size: 12, color: m.feedbackError ? T.danger : T.faint, maxW: w - pad * 2 - 90, fit: true });
49      drawText(ctx, `${text.length}/${MAX}`, w - pad, bottom + 4, { h: 18, size: 12, color: T.faint, align: 'right' });
50      const bw = (w - pad * 2 - 10) / 2;
51      const ready = text.trim().length >= MIN && !m.feedbackBusy;
52      f.button(ctx, pad, h - pad - 40, bw, 40, m.feedbackBusy ? 'Sending' : 'Send', { primary: ready, disabled: !ready, hover: wd.hovered('feedback-send'), size: 15 });
53      wd.hit(pad, h - pad - 40, bw, 40, { name: 'feedback-send', key: 'feedback-send', onClick: () => ready && m.sendFeedback(text.trim()) });
54      f.button(ctx, pad + bw + 10, h - pad - 40, bw, 40, 'Cancel', { hover: wd.hovered('feedback-cancel'), size: 15 });
55      wd.hit(pad + bw + 10, h - pad - 40, bw, 40, { name: 'feedback-cancel', key: 'feedback-cancel', onClick: () => m.closePanel() });
56    },
57  });
58
59  area.addEventListener('input', () => {
60    m.feedbackError = '';
61    panel.invalidate();
62  });
63  area.addEventListener('focus', () => panel.invalidate());
64  area.addEventListener('blur', () => panel.invalidate());
65  area.addEventListener('keydown', (e) => {
66    e.stopPropagation();
67    if (e.key === 'Escape') {
68      e.preventDefault();
69      m.closePanel();
70    } else if (e.key === 'Enter' && (e.ctrlKey || e.metaKey) && area.value.trim().length >= MIN) {
71      e.preventDefault();
72      m.sendFeedback(area.value.trim());
73    }
74  });
75
76  const sync = () => {
77    if (m.panel !== 'feedback' && focused()) area.blur();
78  };
79  const clear = () => {
80    area.value = '';
81    area.blur();
82  };
83  return { widgets: { 'feedback-cta': cta, 'panel-feedback': panel }, sync, clear, focus: () => area.focus() };
84}