Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs #15

Merged
merged 14 commits into from
Mar 5, 2024
Merged

Docs #15

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,15 @@
# String manipulator

Application that applies sequence of manipulations to string

Interactive doc available at https://mishankov.github.io/string-manipulator/docs

Available manipulations:
- Replace
- Prepend
- Append
- Split, get from index
- Compose
- Slice
- Split-compose
- Split-join
62 changes: 9 additions & 53 deletions src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@
<script lang="ts">
import { type TManipulation, applyManipulations } from "./manipulations";
import ManipulationsPanel from "./ManipulationsPanel.svelte";

import TextArea from "./components/TextArea.svelte";
import Heading from "./components/Heading.svelte";

let source = "Try me!\nResult will update automatically"
let result = ""
let manipulations: TManipulation[] = [{
type: "replace",
from: "\\n", to: " -- ", id: "initial"
}]

document.addEventListener("keydown", (event) => {
if (event.ctrlKey && event.key === "Enter") {
result = applyManipulations(source, manipulations)
}
})

$: result = applyManipulations(source, manipulations)
import Router, { type Route, routes } from "./components/Router.svelte";
import App from "./pages/App";
import Docs from "./pages/Docs";

routes.set([
{ location: "/", component: App },
{ location: "/docs", component: Docs }
])
</script>

<div class="panels">
<div class="panel">
<Heading text="Source" />
<TextArea id="source" bind:value={source} />
</div>

<div class="panel">
<Heading text="Manipulations" />
<ManipulationsPanel bind:manipulations />
</div>

<div class="panel">
<Heading text="Result" />
<TextArea id="result" bind:value={result} />
</div>
</div>


<style>
.panels {
display: grid;
grid-template-columns: 1fr 1fr 1fr;
gap: 10px;

padding: 10px;
}

.panel {
display: flex;
flex-direction: column;
gap: 10px;
}
</style>
<Router />
19 changes: 19 additions & 0 deletions src/components/Link.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<script lang="ts">
import { navigate } from "./Router.svelte";

export let link: string;
export let isNavigation = false
</script>

{#if isNavigation}
<a href="{link}" on:click={navigate}><slot/></a>
{:else}
<a href="{link}"><slot/></a>
{/if}


<style>
a {
font-family: monospace;
}
</style>
53 changes: 53 additions & 0 deletions src/components/Router.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<script lang="ts" context="module">
import { onMount, type ComponentType } from "svelte";
import { get, writable } from "svelte/store";

import urlStore from "./url-store";

export interface Route {
location: string
component: ComponentType
}

export const routes = writable<Route[]>();
export const currentRoute = writable<Route>();

function pathWithBase(path: string): string {
if (import.meta.env.BASE_URL === "/") return path
return import.meta.env.BASE_URL + path
}

function currentLocation(pathname: string) {
if (pathname === pathWithBase("/")) return pathname;
if (pathname.endsWith("/")) return pathname.slice(0, pathname.length - 1);

return pathname;
};

function getRoute(href: string, routes: Route[]): Route {
const url = new URL(href);
const route = routes.find(route => pathWithBase(route.location) === currentLocation(url.pathname)) || routes[0]
return route
}

export function navigate(event: MouseEvent) {
event.preventDefault();
const target = event.target as HTMLAnchorElement
window.history.pushState('', '', target["href"]);
const route = getRoute(target["href"], get(routes));
currentRoute.set(route);
};
</script>

<script lang="ts">
onMount(() => {
currentRoute.set(getRoute($urlStore.href, $routes));
});

$: currentRoute.set(getRoute($urlStore.href, $routes))
</script>

{#if $currentRoute}
<svelte:component this={$currentRoute.component}/>
{/if}

31 changes: 31 additions & 0 deletions src/components/url-store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Original source: github.com/bluwy/svelte-url/blob/master/src/url.js

import { derived, writable } from "svelte/store";

export function createUrlStore() {
const href = writable(window.location.href);

const originalPushState = history.pushState;
const originalReplaceState = history.replaceState;

const updateHref = () => href.set(window.location.href);

history.pushState = function (data, unused, url) {
originalPushState.apply(this, [data, unused, url]);
updateHref();
};

history.replaceState = function (data, unused, url) {
originalReplaceState.apply(this, [data, unused, url]);
updateHref();
};

window.addEventListener("popstate", updateHref);
window.addEventListener("hashchange", updateHref);

return {
subscribe: derived(href, ($href) => new URL($href)).subscribe,
};
}

export default createUrlStore();
8 changes: 8 additions & 0 deletions src/manipulations/components/Append.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@
<div>
<span>Suffix </span><input bind:value={suffix} />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
8 changes: 8 additions & 0 deletions src/manipulations/components/Compose.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@
<div>
<span>Replace placeholder</span> <input bind:value={placeholder}> <span>in pattern</span> <textarea bind:value={pattern} />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
8 changes: 8 additions & 0 deletions src/manipulations/components/Prepend.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@
<span>Prefix </span><input bind:value={prefix} />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>

9 changes: 9 additions & 0 deletions src/manipulations/components/Replace.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@
<div>
<span>Replace from</span> <input bind:value={from} /> <span>to</span> <input bind:value={to} />
</div>


<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
8 changes: 8 additions & 0 deletions src/manipulations/components/Slice.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,11 @@
<div>
<span>Slice from</span> <input bind:value={start} type="number"/> <span>to</span> <input bind:value={end} type="number" />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
10 changes: 9 additions & 1 deletion src/manipulations/components/SplitCompose.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,13 @@
</script>

<div>
<span>Split by</span> <input bind:value={splitString}/> <span> and replace placeholder</span> <input bind:value={placeholder} /> <span>in pattern</span> <textarea bind:value={pattern} />
<span>Split by</span> <input bind:value={splitString}/> <span>and replace placeholder</span> <input bind:value={placeholder} /> <span>in pattern</span> <textarea bind:value={pattern} />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
10 changes: 9 additions & 1 deletion src/manipulations/components/SplitGetFromIndex.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@
</script>

<div>
<span>Split by </span><input bind:value={splitString} /><span>, get from </span><input bind:value={index} />
<span>Split by </span><input bind:value={splitString} /><span>get from </span><input bind:value={index} />
</div>

<style>
div {
display: flex;
flex-direction: column;
gap: 5px;
}
</style>
66 changes: 66 additions & 0 deletions src/pages/App/App.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<script lang="ts">
import { type TManipulation, applyManipulations } from "../../manipulations";
import ManipulationsPanel from "./ManipulationsPanel.svelte";

import TextArea from "../../components/TextArea.svelte";
import Heading from "../../components/Heading.svelte";
import Link from "../../components/Link.svelte";

let source = "Try me!\nResult will be updated automatically"
let result = ""
let manipulations: TManipulation[] = [{
type: "replace",
from: "\\n", to: " -- ", id: "initial"
}]

document.addEventListener("keydown", (event) => {
if (event.ctrlKey && event.key === "Enter") {
result = applyManipulations(source, manipulations)
}
})

$: result = applyManipulations(source, manipulations)
</script>

<div class="panels">
<div class="panel">
<Heading text="Source" />
<TextArea id="source" bind:value={source} />
</div>

<div class="panel">
<div class="header-with-link">
<Heading text="Manipulations" /> <Link link="/docs" isNavigation>Docs</Link>
</div>
<ManipulationsPanel bind:manipulations />
</div>

<div class="panel">
<Heading text="Result" />
<TextArea id="result" bind:value={result} />
</div>
</div>


<style>
.panels {
display: grid;
grid-template-columns: 1fr auto 1fr;
gap: 10px;

padding: 10px;
}

.panel {
display: flex;
flex-direction: column;
gap: 10px;
}

.header-with-link {
display: flex;
flex-direction: row;
gap: 10px;
align-items: end;
}
</style>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">
import type { TManipulation } from "./manipulations";
import { ManipulationsList } from "./manipulations";
import type { TManipulation } from "../../manipulations";
import { ManipulationsList } from "../../manipulations";

export let manipulations: TManipulation[] = []
</script>
Expand Down
3 changes: 3 additions & 0 deletions src/pages/App/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import App from "./App.svelte";

export default App;
Loading