13 changed files with 155 additions and 21 deletions
			
			
		@ -0,0 +1,66 @@
				@@ -0,0 +1,66 @@
					 | 
				
			||||
<script> | 
				
			||||
    import { useTinyRouter } from 'svelte-tiny-router'; | 
				
			||||
    import { api }           from '../../urls.svelte'; | 
				
			||||
    import { t }             from '../../translations.svelte'; | 
				
			||||
 | 
				
			||||
    import Markdown          from '../../Components/MarkdownEditor.svelte'; | 
				
			||||
 | 
				
			||||
    let content = $state({source:null,rendered:null}); | 
				
			||||
    let error   = $state(null); | 
				
			||||
    let router  = useTinyRouter(); | 
				
			||||
    let timer   = null; | 
				
			||||
    let title   = $state(null); | 
				
			||||
 | 
				
			||||
    async function onsubmit(ev){ | 
				
			||||
        ev.preventDefault(); | 
				
			||||
        ev.stopPropagation(); | 
				
			||||
        const url = api(`wiki/${title}`); | 
				
			||||
        const res = await fetch(url,{ | 
				
			||||
            credentials:'include', | 
				
			||||
            method:'POST', | 
				
			||||
            body:content.source | 
				
			||||
        }); | 
				
			||||
        if (res.ok){ | 
				
			||||
            router.navigate(`/wiki/${title}/view`); | 
				
			||||
        } else { | 
				
			||||
            error = await res.text(); | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    function checkTitle(){ | 
				
			||||
        if (title) { | 
				
			||||
            if (timer) clearTimeout(timer); | 
				
			||||
            timer = setTimeout(requestTitle,1000); | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    async function requestTitle(){ | 
				
			||||
        timer = null; | 
				
			||||
        var url = api(`wiki/available/${title}`); | 
				
			||||
        var res = await fetch(url,{credentials:'include'}); | 
				
			||||
        const body = await res.text(); | 
				
			||||
        if (res.ok){ | 
				
			||||
            error = body == 'true' ? null : t('title_not_available',{title:title}) | 
				
			||||
        } else { | 
				
			||||
            error = body; | 
				
			||||
            if (!error) error = t('failed_to_check_availability'); | 
				
			||||
        } | 
				
			||||
    } | 
				
			||||
 | 
				
			||||
    $effect(checkTitle) | 
				
			||||
</script> | 
				
			||||
 | 
				
			||||
<h1>{t('create_new_object',{object:t('page')})}</h1> | 
				
			||||
{#if error} | 
				
			||||
<span class="error">{error}</span> | 
				
			||||
{/if} | 
				
			||||
<form {onsubmit}> | 
				
			||||
    <label> | 
				
			||||
        {t('Name')} | 
				
			||||
        <input type="text" bind:value={title} /> | 
				
			||||
    </label> | 
				
			||||
    <label> | 
				
			||||
        {t('content')} | 
				
			||||
        <Markdown bind:value={content} simple={true} /> | 
				
			||||
    </label> | 
				
			||||
</form> | 
				
			||||
					Loading…
					
					
				
		Reference in new issue