refac
This commit is contained in:
@@ -13,6 +13,7 @@
|
|||||||
getSkillById,
|
getSkillById,
|
||||||
getSkillItems,
|
getSkillItems,
|
||||||
exportSkills,
|
exportSkills,
|
||||||
|
createNewSkill,
|
||||||
deleteSkillById,
|
deleteSkillById,
|
||||||
toggleSkillById
|
toggleSkillById
|
||||||
} from '$lib/apis/skills';
|
} from '$lib/apis/skills';
|
||||||
@@ -198,30 +199,66 @@
|
|||||||
bind:this={importInputElement}
|
bind:this={importInputElement}
|
||||||
bind:files={importFiles}
|
bind:files={importFiles}
|
||||||
type="file"
|
type="file"
|
||||||
accept=".md"
|
accept=".md,.json"
|
||||||
hidden
|
hidden
|
||||||
on:change={() => {
|
on:change={() => {
|
||||||
if (importFiles && importFiles.length > 0) {
|
if (importFiles && importFiles.length > 0) {
|
||||||
const reader = new FileReader();
|
const file = importFiles[0];
|
||||||
reader.onload = (event) => {
|
const ext = file.name.split('.').pop()?.toLowerCase();
|
||||||
const mdContent = event.target?.result;
|
|
||||||
if (typeof mdContent === 'string') {
|
if (ext === 'json') {
|
||||||
const fm = parseFrontmatter(mdContent);
|
// JSON import: create skills via API
|
||||||
const fileName = importFiles[0].name.replace(/\.md$/, '');
|
const reader = new FileReader();
|
||||||
const rawName = fm.name || fileName;
|
reader.onload = async (event) => {
|
||||||
const displayName = formatSkillName(rawName);
|
try {
|
||||||
sessionStorage.skill = JSON.stringify({
|
const content = event.target?.result;
|
||||||
name: displayName,
|
if (typeof content !== 'string') return;
|
||||||
id: fm.name || '', // Use raw frontmatter name as ID if available
|
|
||||||
description: fm.description || '',
|
const parsedSkills = JSON.parse(content);
|
||||||
content: mdContent,
|
const items = Array.isArray(parsedSkills) ? parsedSkills : [parsedSkills];
|
||||||
is_active: true,
|
|
||||||
access_grants: []
|
for (const skill of items) {
|
||||||
});
|
await createNewSkill(localStorage.token, skill).catch(
|
||||||
goto('/workspace/skills/create');
|
(error) => {
|
||||||
}
|
toast.error(`${error}`);
|
||||||
};
|
}
|
||||||
reader.readAsText(importFiles[0]);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
toast.success($i18n.t('Skill imported successfully'));
|
||||||
|
page = 1;
|
||||||
|
loadSkillItems();
|
||||||
|
_skills.set(await getSkills(localStorage.token));
|
||||||
|
} catch (e) {
|
||||||
|
toast.error($i18n.t('Invalid JSON file'));
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.readAsText(file);
|
||||||
|
} else {
|
||||||
|
// Markdown import: parse frontmatter and open in editor
|
||||||
|
const reader = new FileReader();
|
||||||
|
reader.onload = (event) => {
|
||||||
|
const mdContent = event.target?.result;
|
||||||
|
if (typeof mdContent === 'string') {
|
||||||
|
const fm = parseFrontmatter(mdContent);
|
||||||
|
const fileName = file.name.replace(/\.md$/, '');
|
||||||
|
const rawName = fm.name || fileName;
|
||||||
|
const displayName = formatSkillName(rawName);
|
||||||
|
sessionStorage.skill = JSON.stringify({
|
||||||
|
name: displayName,
|
||||||
|
id: fm.name || '',
|
||||||
|
description: fm.description || '',
|
||||||
|
content: mdContent,
|
||||||
|
is_active: true,
|
||||||
|
access_grants: []
|
||||||
|
});
|
||||||
|
goto('/workspace/skills/create');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
reader.readAsText(file);
|
||||||
|
}
|
||||||
|
|
||||||
|
importInputElement.value = '';
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user