-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'refactor-customtags' of github.com:candela97/AugmentedS…
…team into candela97-refactor-customtags
- Loading branch information
Showing
11 changed files
with
164 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/js/Content/Features/Community/EditGuide/Components/AddTagForm.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<svelte:options accessors /> | ||
|
||
<script lang="ts"> | ||
import {__enterTag} from "@Strings/_strings"; | ||
import {L} from "@Core/Localization/Localization"; | ||
export let tag: string; | ||
</script> | ||
|
||
|
||
<input placeholder="{L(__enterTag)}" bind:value={tag} on:change> | ||
|
||
|
||
<style> | ||
input { | ||
text-transform: capitalize; | ||
} | ||
input::placeholder { | ||
text-transform: none; | ||
} | ||
</style> |
118 changes: 118 additions & 0 deletions
118
src/js/Content/Features/Community/EditGuide/FCustomTags.svelte
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
<svelte:options immutable={false} /> | ||
|
||
<script lang="ts"> | ||
import {__addTag, __customTags} from "@Strings/_strings"; | ||
import {L} from "@Core/Localization/Localization"; | ||
import LocalStorage from "@Core/Storage/LocalStorage"; | ||
import HTML from "@Core/Html/Html"; | ||
import SteamFacade from "@Content/Modules/Facades/SteamFacade"; | ||
import RequestData from "@Content/Modules/RequestData"; | ||
import {onMount} from "svelte"; | ||
import AddTagForm from "./Components/AddTagForm.svelte"; | ||
let customTags: Set<string> = new Set(); // Use Set to enforce unique tags | ||
async function addTags(): Promise<void> { | ||
const params = new URLSearchParams(window.location.search); | ||
let tags: string[]; | ||
// id param only appears when editing existing guides; fetch tags from guide page (add preview=true to support unpublished) | ||
if (params.has("id")) { | ||
const data = await RequestData.getText(`https://steamcommunity.com/sharedfiles/filedetails/?id=${params.get("id")}&preview=true`); | ||
const dom = HTML.toDom(data); | ||
tags = [...dom.querySelectorAll(".workshopTags > a")].map(node => node.textContent!.trim()); | ||
} else { | ||
tags = (await LocalStorage.get("guide_tags")) ?? []; | ||
} | ||
for (const tag of tags) { | ||
const node = document.querySelector<HTMLInputElement>(`[name="tags[]"][value="${tag.replace(/"/g, '\\"')}"]`); | ||
if (node) { | ||
node.checked = true; | ||
} else { | ||
customTags.add(tag); | ||
} | ||
} | ||
customTags = customTags; | ||
} | ||
async function showDialog(): Promise<void> { | ||
let form: AddTagForm|undefined; | ||
let tag: string = ""; | ||
const observer = new MutationObserver(() => { | ||
const modal = document.querySelector("#as_tag_form"); | ||
if (modal) { | ||
form = new AddTagForm({ | ||
target: modal, | ||
props: {tag} | ||
}); | ||
form.$on("change", () => { | ||
tag = form!.tag; | ||
}); | ||
observer.disconnect(); | ||
} | ||
}); | ||
observer.observe(document.body, { | ||
childList: true | ||
}); | ||
const response = await SteamFacade.showConfirmDialog( | ||
L(__customTags), | ||
`<div id="as_tag_form"></div>` | ||
); | ||
form?.$destroy(); | ||
if (response === "OK") { | ||
if (tag.trim() === "") { | ||
return; | ||
} | ||
tag = tag.slice(0, 1).toUpperCase() + tag.slice(1); | ||
customTags.add(tag); | ||
customTags = customTags; | ||
} | ||
} | ||
const submitBtn = document.querySelector("[href*=SubmitGuide]"); | ||
if (submitBtn) { | ||
submitBtn.removeAttribute("href"); | ||
submitBtn.addEventListener("click", async () => { | ||
const recentTags: string[] = [...document.querySelectorAll<HTMLInputElement>("[name='tags[]']:checked")] | ||
.map(node => node.value); | ||
await LocalStorage.set("guide_tags", recentTags); | ||
SteamFacade.submitGuide(); | ||
}); | ||
} | ||
onMount(() => { | ||
addTags(); | ||
}); | ||
</script> | ||
|
||
|
||
<div class="tag_category_container" id="checkboxgroup_2"> | ||
<div class="tag_category_desc">{L(__customTags)}</div> | ||
{#each customTags as tag} | ||
<div> | ||
<input type="checkbox" name="tags[]" value="{tag}" class="inputTagsFilter" checked> | ||
{tag} | ||
</div> | ||
{/each} | ||
<!-- svelte-ignore a11y-click-events-have-key-events a11y-missing-attribute --> | ||
<a class="btn_blue_white_innerfade btn_small_thin as_add_tag" on:click={showDialog} role="button" tabindex="0"> | ||
<span>{L(__addTag)}</span> | ||
</a> | ||
</div> | ||
|
||
|
||
<style> | ||
.as_add_tag { | ||
margin-top: 8px; | ||
} | ||
</style> |
78 changes: 10 additions & 68 deletions
78
src/js/Content/Features/Community/EditGuide/FCustomTags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,81 +1,23 @@ | ||
import {__addTag, __customTags, __enterTag} from "@Strings/_strings"; | ||
import {L} from "@Core/Localization/Localization"; | ||
import self_ from "./FCustomTags.svelte"; | ||
import Feature from "@Content/Modules/Context/Feature"; | ||
import type CEditGuide from "@Content/Features/Community/EditGuide/CEditGuide"; | ||
import DOMHelper from "@Content/Modules/DOMHelper"; | ||
import HTML from "@Core/Html/Html"; | ||
import LocalStorage from "@Core/Storage/LocalStorage"; | ||
|
||
export default class FCustomTags extends Feature<CEditGuide> { | ||
|
||
override apply(): void { | ||
this._addTags(); | ||
this._rememberTags(); | ||
} | ||
|
||
_addTags(): void { | ||
const langSection = document.querySelector("#checkboxgroup_1"); | ||
if (!langSection) { return; } | ||
|
||
// @ts-ignore | ||
document.addEventListener("as_addTag", (e: CustomEvent<string>) => { | ||
this._addTag(e.detail, true); | ||
}); | ||
|
||
HTML.afterEnd(langSection, | ||
`<div class="tag_category_container" id="checkboxgroup_2"> | ||
<div class="tag_category_desc">${L(__customTags)}</div> | ||
<div><a style="margin-top: 8px;" class="btn_blue_white_innerfade btn_small_thin" id="es_add_tag"> | ||
<span>${L(__addTag)}</span> | ||
</a></div> | ||
</div>`); | ||
|
||
DOMHelper.insertScript("scriptlets/Community/EditGuide/customTags.js", { | ||
customTags: L(__customTags), | ||
enterTag: L(__enterTag) | ||
}); | ||
} | ||
|
||
async _rememberTags(): Promise<void> { | ||
|
||
const submitBtn = document.querySelector("[href*=SubmitGuide]"); | ||
if (!submitBtn) { return; } | ||
|
||
const params = new URLSearchParams(window.location.search); | ||
const curId = params.get("id") ?? "recent"; | ||
const savedTags: Record<string, string[]> = (await LocalStorage.get("es_guide_tags")) ?? {}; | ||
if (!savedTags[curId]) { | ||
savedTags[curId] = savedTags.recent ?? []; | ||
const anchor = document.querySelector("#checkboxgroup_1"); | ||
if (!anchor) { | ||
throw new Error("Node not found"); | ||
} | ||
|
||
for (const [id, tags] of Object.entries(savedTags)) { | ||
for (const tag of tags) { | ||
const node = document.querySelector<HTMLInputElement>(`[name="tags[]"][value="${tag.replace(/"/g, '\\"')}"]`); | ||
if (node && curId === id) { | ||
node.checked = true; | ||
} else if (!node) { | ||
this._addTag(tag, curId === id); | ||
} | ||
} | ||
// Make language options multi-selectable | ||
for (const tag of anchor.querySelectorAll<HTMLInputElement>("[name='tags[]']")) { | ||
tag.type = "checkbox"; | ||
} | ||
|
||
submitBtn.removeAttribute("href"); | ||
submitBtn.addEventListener("click", () => { | ||
savedTags.recent = []; | ||
savedTags[curId] = Array.from( | ||
document.querySelectorAll<HTMLInputElement>("[name='tags[]']:checked") | ||
).map(node => node.value); | ||
|
||
LocalStorage.set("es_guide_tags", savedTags); | ||
|
||
DOMHelper.insertScript("scriptlets/Community/EditGuide/submitGuide.js"); | ||
new self_({ | ||
target: anchor.parentElement!, | ||
anchor: anchor.nextElementSibling ?? undefined, | ||
}); | ||
} | ||
|
||
_addTag(name: string, checked: boolean = true): void { | ||
const _name = HTML.escape(name); | ||
const attr = checked ? " checked" : ""; | ||
const tag = `<div><input type="checkbox" name="tags[]" value="${_name}" class="inputTagsFilter"${attr}>${_name}</div>`; | ||
HTML.beforeBegin("#es_add_tag", tag); | ||
} | ||
} |
11 changes: 0 additions & 11 deletions
11
src/js/Content/Features/Community/EditGuide/FMultiLanguageGuide.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters