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

Fix tagging #492

Merged
merged 4 commits into from
Nov 27, 2023
Merged
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
2 changes: 0 additions & 2 deletions src/devhub/components/molecule/SimpleMDE.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,7 @@ const code = `
initialText: event.data.content }));
isEditorInitialized = true;
} else {
console.log(event);
if (event.data.handler === 'autocompleteSelected') {
console.log("we're in");
codeMirrorInstance.getDoc().setValue(event.data.content);
}
}
Expand Down
77 changes: 51 additions & 26 deletions src/devhub/page/create.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
/* INCLUDE: "core/lib/autocomplete" */

State.init({
seekingFunding: false,
author_id: context.accountId,
// Should be a list of objects with field "name".
labels,
// Should be a list of labels as strings.
// Both of the label structures should be modified together.
labelStrings,
postType: "Idea",
name: props.name ?? "",
description: props.description ?? "",
amount: props.amount ?? "",
token: props.token ?? "USDT",
supervisor: props.supervisor ?? "neardevdao.near",
githubLink: props.githubLink ?? "",
warning: "",
waitForDraftStateRestore: true,
mentionInput: "", // text next to @ tag
mentionsArray: [], // all the mentions in the description
});

const autocompleteEnabled = true;

const AutoComplete = styled.div`
Expand All @@ -10,20 +32,43 @@ const AutoComplete = styled.div`
`;

function textareaInputHandler(value) {
const showAccountAutocomplete = /@[\w][^\s]*$/.test(value);
const words = value.split(/\s+/);
const allMentiones = words
.filter((word) => word.startsWith("@"))
.map((mention) => mention.slice(1));
const newMentiones = allMentiones.filter(
(item) => !state.mentionsArray.includes(item)
);

State.update((lastKnownState) => ({
...lastKnownState,
text: value,
showAccountAutocomplete,
showAccountAutocomplete: newMentiones?.length > 0,
mentionsArray: allMentiones,
mentionInput: newMentiones?.[0] ?? "",
}));
}

function autoCompleteAccountId(id) {
let description = state.description.replace(/[\s]{0,1}@[^\s]*$/, "");
description = `${description} @${id}`.trim() + " ";
// to make sure we update the @ at correct index
let currentIndex = 0;
const updatedDescription = state.description.replace(
/(?:^|\s)(@[^\s]*)/g,
(match) => {
if (currentIndex === state.mentionsArray.indexOf(state.mentionInput)) {
currentIndex++;
return ` @${id}`;
} else {
currentIndex++;
return match;
}
}
);

State.update((lastKnownState) => ({
...lastKnownState,
description,
handler: "autocompleteSelected",
description: updatedDescription,
showAccountAutocomplete: false,
}));
}
Expand Down Expand Up @@ -51,26 +96,6 @@ const labels = labelStrings.map((s) => {
return { name: s };
});

State.init({
seekingFunding: false,

author_id: context.accountId,
// Should be a list of objects with field "name".
labels,
// Should be a list of labels as strings.
// Both of the label structures should be modified together.
labelStrings,
postType: "Idea",
name: props.name ?? "",
description: props.description ?? "",
amount: props.amount ?? "",
token: props.token ?? "USDT",
supervisor: props.supervisor ?? "neardevdao.near",
githubLink: props.githubLink ?? "",
warning: "",
waitForDraftStateRestore: true,
});

if (state.waitForDraftStateRestore) {
const draftstatestring = Storage.privateGet(DRAFT_STATE_STORAGE_KEY);
if (draftstatestring != null) {
Expand Down Expand Up @@ -315,7 +340,7 @@ const descriptionDiv = (
<Widget
src="${REPL_NEAR}/widget/AccountAutocomplete"
props={{
term: state.text.split("@").pop(),
term: state.mentionInput,
onSelect: autoCompleteAccountId,
onClose: () => State.update({ showAccountAutocomplete: false }),
}}
Expand Down