Skip to content

Commit

Permalink
sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
daslyfe committed Oct 25, 2024
1 parent dfd3675 commit 222c95d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
15 changes: 12 additions & 3 deletions website/src/metadata_parser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
const ALLOW_MANY = ['by', 'url', 'genre', 'license'];

export function getMetadata(raw_code) {
const comment_regexp = /\/\*([\s\S]*?)\*\/|\/\/(.*)$/gm;
const comments = [...raw_code.matchAll(comment_regexp)].map((c) => (c[1] || c[2] || '').trim());
const tags = {};

const tags = {};
if (raw_code == null) {
return tags
}
const comment_regexp = /\/\*([\s\S]*?)\*\/|\/\/(.*)$/gm;
let comments = []
try {
comments = [...raw_code.matchAll?.(comment_regexp) ?? []].map((c) => (c[1] || c[2] || '').trim());
} catch (e) {
console.error(e)
}

const [prefix, title] = (comments[0] || '').split('"');
if (prefix.trim() === '' && title !== undefined) {
tags['title'] = title;
Expand Down
5 changes: 2 additions & 3 deletions website/src/repl/components/panel/PatternsTab.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ function PatternButtons({ patterns, activePattern, onClick, started }) {

function ActionButton({ children, onClick, label, labelIsHidden }) {
return (
<button className="hover:opacity-50 text-nowrap" onClick={onClick} title={label}>
<button aria-label={label} className="hover:opacity-50 text-nowrap" onClick={onClick} title={label}>
{labelIsHidden !== true && label}
{children}
</button>
Expand Down Expand Up @@ -147,8 +147,7 @@ export function PatternsTab({ context }) {
<ActionButton
label="delete-all"
onClick={() => {
const { data } = userPattern.clearAll();
updateCodeWindow(data);
userPattern.clearAll(updateCodeWindow);
}}
/>
</div>
Expand Down
15 changes: 12 additions & 3 deletions website/src/user_pattern_utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ export const userPattern = {
const newPattern = this.create();
return this.update(newPattern.id, newPattern.data);
},
sortAlphabetical() {
const userPatterns = this.getAll();
const keys = Object.keys(userPatterns).sort((a, b) => b.localeCompare(a));
const sortedPatterns = {};
keys.forEach((key) => (sortedPatterns[key] = userPatterns[key]));
setUserPatterns(sortedPatterns);
},

update(id, data) {
const userPatterns = this.getAll();
Expand All @@ -129,7 +136,7 @@ export const userPattern = {
const newPattern = this.create();
return this.update(newPattern.id, { ...newPattern.data, code: data.code });
},
clearAll() {
clearAll(onSuccess) {
confirmDialog(`This will delete all your patterns. Are you really sure?`).then((r) => {
if (r == false) {
return;
Expand All @@ -138,10 +145,10 @@ export const userPattern = {
setUserPatterns({});

if (viewingPatternData.collection !== this.collection) {
return { id: viewingPatternData.id, data: viewingPatternData };
onSuccess({ id: viewingPatternData.id, data: viewingPatternData });
}
setActivePattern(null);
return this.create();
onSuccess(this.create());
});
},
delete(id) {
Expand Down Expand Up @@ -170,6 +177,7 @@ export const createPatternID = () => {

export async function importPatterns(fileList) {
const files = Array.from(fileList);

await Promise.all(
files.map(async (file, i) => {
const content = await file.text();
Expand All @@ -182,6 +190,7 @@ export async function importPatterns(fileList) {
}
}),
);
userPattern.sortAlphabetical();
logger(`import done!`);
}

Expand Down

0 comments on commit 222c95d

Please sign in to comment.