Gets the name of a number, even if it's stupidly big. Supersedes TheoryOfNekomata/number-name.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

191 regels
4.6 KiB

  1. <!DOCTYPE html>
  2. <html lang="en-PH">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>@modal-sh/numerica-core</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <style>
  8. .main-form {
  9. position: fixed;
  10. top: 0;
  11. left: 0;
  12. width: 100%;
  13. height: 100%;
  14. display: flex;
  15. flex-direction: column;
  16. align-items: stretch;
  17. }
  18. .main-form > fieldset {
  19. display: contents;
  20. }
  21. .main-form > fieldset > div {
  22. padding: 1rem;
  23. box-sizing: border-box;
  24. display: flex;
  25. gap: 1rem;
  26. }
  27. .main-form > fieldset > legend {
  28. position: absolute;
  29. left: -999999px;
  30. }
  31. .main-form > div {
  32. flex-direction: column;
  33. display: flex;
  34. flex: auto;
  35. }
  36. .main-form > div > textarea {
  37. flex: auto;
  38. width: 100%;
  39. height: 0;
  40. font-size: 2rem;
  41. padding: 1rem;
  42. border: 0;
  43. outline: 0;
  44. resize: none;
  45. box-sizing: border-box;
  46. }
  47. .main-form > div > textarea:invalid {
  48. color: red;
  49. }
  50. .checkbox > input {
  51. position: absolute;
  52. left: -999999px;
  53. }
  54. .checkbox > input + span {
  55. display: inline-flex;
  56. align-items: center;
  57. justify-content: center;
  58. height: 3rem;
  59. padding: 0 1rem;
  60. border: 1px solid #ccc;
  61. border-radius: 0.25rem;
  62. cursor: pointer;
  63. }
  64. .checkbox > input:checked + span {
  65. border-color: #000;
  66. background-color: #000;
  67. color: #fff;
  68. }
  69. @media (min-width: 1080px) {
  70. .main-form > div {
  71. flex-direction: row;
  72. }
  73. .main-form > div > textarea {
  74. width: 0;
  75. height: 100%;
  76. }
  77. }
  78. </style>
  79. </head>
  80. <body>
  81. <form id="mainForm" class="main-form">
  82. <fieldset>
  83. <legend>
  84. Options
  85. </legend>
  86. <div>
  87. <label class="checkbox">
  88. <input type="checkbox" name="shortenMillia">
  89. <span>Shorten millia</span>
  90. </label><label class="checkbox">
  91. <input type="checkbox" name="addTensDashes" checked>
  92. <span>Add dashes to tens</span>
  93. </label><label class="checkbox">
  94. <input type="checkbox" name="oneGroupPerLine" checked>
  95. <span>One group per line</span>
  96. </label><!--<label class="checkbox">
  97. <input type="groupDigits" name="groupDigits" checked>
  98. <span>Group digits</span>
  99. </label>-->
  100. </div>
  101. </fieldset>
  102. <div>
  103. <textarea aria-label="Number" name="number" placeholder="123"></textarea>
  104. <textarea aria-label="Name" name="name" placeholder="one hundred twenty three"></textarea>
  105. </div>
  106. </form>
  107. <script type="module">
  108. import { stringify, parse } from './numerica.js';
  109. function MainForm(el) {
  110. const numberInput = el.querySelector('[name="number"]');
  111. const nameInput = el.querySelector('[name="name"]');
  112. const addTensDashesCheckbox = el.querySelector('[name="addTensDashes"]');
  113. const shortenMilliaCheckbox = el.querySelector('[name="shortenMillia"]');
  114. const oneGroupPerLineCheckbox = el.querySelector('[name="oneGroupPerLine"]');
  115. const options = {
  116. stringify: {
  117. makeGroupOptions: {
  118. shortenMillia: false,
  119. addTensDashes: true,
  120. },
  121. finalizeOptions: {
  122. oneGroupPerLine: true,
  123. },
  124. },
  125. parse: {
  126. type: 'bigint',
  127. }
  128. };
  129. numberInput.addEventListener('input', (e) => {
  130. e.currentTarget.setCustomValidity('');
  131. nameInput.value = '';
  132. if (e.currentTarget.value.trim() === '') {
  133. return;
  134. }
  135. try {
  136. nameInput.value = stringify(e.currentTarget.value, options.stringify);
  137. } catch {
  138. e.currentTarget.setCustomValidity('Invalid number.');
  139. }
  140. });
  141. nameInput.addEventListener('input', (e) => {
  142. e.currentTarget.setCustomValidity('');
  143. numberInput.value = '';
  144. if (e.currentTarget.value.trim() === '') {
  145. return;
  146. }
  147. try {
  148. numberInput.value = parse(e.currentTarget.value, options.parse)
  149. .toString();
  150. // TODO group digits function from system.
  151. } catch {
  152. e.currentTarget.setCustomValidity('Invalid name.');
  153. }
  154. });
  155. addTensDashesCheckbox.addEventListener('change', (e) => {
  156. options.stringify.makeGroupOptions.addTensDashes = e.currentTarget.checked;
  157. numberInput.dispatchEvent(new Event('input'));
  158. });
  159. shortenMilliaCheckbox.addEventListener('change', (e) => {
  160. options.stringify.makeGroupOptions.shortenMillia = e.currentTarget.checked;
  161. numberInput.dispatchEvent(new Event('input'));
  162. });
  163. oneGroupPerLineCheckbox.addEventListener('change', (e) => {
  164. options.stringify.finalizeOptions.oneGroupPerLine = e.currentTarget.checked;
  165. numberInput.dispatchEvent(new Event('input'));
  166. });
  167. }
  168. const mainForm = window.document.getElementById('mainForm');
  169. new MainForm(mainForm);
  170. </script>
  171. </body>
  172. </html>