summaryrefslogtreecommitdiff
path: root/src/lib/component/Remarks.svelte
blob: 6633654f83bde62155d22fcf21a2a00287633138 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<script lang="ts">
  /*** STATE -------------------------------------------- ***/
  const remarks = [
    "WR-023.txt",
    "WR-022.txt",
    "WR-021.txt",
    "WR-020.txt",
    "WR-019.txt",
    "WR-018.txt",
    "WR-017.txt",
    "WR-016.txt",
    "WR-015.txt",
    "WR-014.txt",
    "WR-013.txt",
    "WR-012.txt",
    "WR-011.txt",
    "WR-010.txt",
    "WR-009.txt",
    "WR-008.txt",
    "WR-007.txt",
    "WR-006.txt",
    "WR-005.txt",
    "WR-004.txt",
    "WR-003.txt",
    "WR-002.txt",
    "WR-001.txt"
  ];

  let selectedRemark: string;
  let selectedRemarkContent: string;

  /*** HELPER ------------------------------------------- ***/
  function processNote(filename: string): string {
    const extensionRegex = /\.[^.]+$/;         /*** file extension ***/
    const specialCharsRegex = /[^a-zA-Z0-9]/g; /*** special characters ***/
    const extensionMatch = filename.match(extensionRegex);
    let processedFilename = filename;
    let wrappedExtension = "";

    if (extensionMatch) {
      processedFilename = filename.slice(0, extensionMatch.index);
      wrappedExtension = extensionMatch[0].replace(extensionRegex, (match) => `<span class="special-char">${match}</span>`);
    }

    return processedFilename.replace(specialCharsRegex, (match) => `<span class="special-char">${match}</span>`) + wrappedExtension;
  }

  async function showNote(slug: string) {
    if (slug === selectedRemark) {
      document.querySelector("li.active")!.classList.remove("active");
      selectedRemark = ""; /*** toggle ***/
    } else {
      selectedRemarkContent = "\nloading…\n";
      selectedRemark = slug;

      try {
        const response = await fetch("/api/remarks.json", {
          body: JSON.stringify({ filename: slug }),
          headers: {
            "Accept": "application/json",
            "Content-Type": "application/json"
          },
          method: "POST"
        });

        const { content } = await response.json();
        selectedRemarkContent = content;
      } catch(error) {
        console.error(error);
      }
    }
  }
</script>

<style lang="scss">
  h2 {
    margin: 0 0 calc(var(--padding)* 2); padding: var(--padding) calc(var(--padding) * 2);

    background-color: var(--color-border);
    color: var(--uchu-yin-7);
    font-size: 1rem;
    line-height: inherit;
    position: sticky;
    top: 0;
    z-index: 1;
  }

  ul {
    line-height: 1;
    margin-left: calc(var(--list-indentation) / 2);
    padding: 0 var(--list-indentation) calc(var(--list-indentation) / 2) 0;

    li {
      margin: 0; padding: 0 0 var(--baseline) calc(var(--baseline) * 2);
      position: relative;

      &::before,
      &::after {
        background-color: var(--color-border);
        content: "";
        left: 0;
        position: absolute;
      }

      &::before {
        width: calc(var(--list-indentation) / 2); height: 1px;
        top: calc(var(--list-indentation) / 4);
      }

      &::after {
        top: calc(var(--list-indentation) * -0.75);
        width: 1px;
      }

      &:not(.active) {
        &::after {
          height: var(--list-indentation);
        }
      }

      &.active::after {
        height: 100%;
      }
    }
  }

  button {
    cursor: pointer;

    &:hover {
      color: var(--uchu-yin-4);
      text-decoration: underline var(--uchu-yin-2);
    }
  }

  .content {
    line-height: 1.55;
    position: relative;
    white-space: pre-wrap;

    text-overflow: ellipsis;
    overflow-x: hidden;

    &::before {
      width: 1px; height: calc(100% + 0.75rem);
      top: -0.75rem; left: calc(var(--list-indentation) * -0.75);

      background-color: var(--color-border);
      content: "";
      position: absolute;
    }
  }

  :global(.date) {
    color: var(--uchu-yin-3);
  }

  :global(.special-char) {
    color: var(--uchu-yin-3);
  }
</style>

<h2>
  <a href="https://blog.webb.page/remarks" target="_blank">blog.webb.page/remarks</a>
</h2>

<ul>
  {#each remarks as remark}
    <li class:active={selectedRemark === remark}>
      <button on:click={() => showNote(remark)}>{@html processNote(remark)}</button>

      {#if selectedRemark === remark}
        <div class="content">
          {@html selectedRemarkContent}
        </div>
      {/if}
    </li>
  {/each}
</ul>