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

Feature/999 simple markdown editor #1006

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ const Compose = ({
"${REPL_DEVHUB}/widget/devhub.components.molecule.SimpleMDE"
}
props={{
instance: props.instance,
data: { handler: state.handler, content: state.data },
onChange: (content) => {
State.update({ data: content, handler: "update" });
Expand Down Expand Up @@ -163,6 +164,7 @@ const Compose = ({
"${REPL_DEVHUB}/widget/devhub.components.molecule.SimpleMDE"
}
props={{
instance: props.instance,
data: { handler: state.handler, content: state.data },
onChange: (content) => {
State.update({ data: content, handler: "update" });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
const { getLinkUsingCurrentGateway } = VM.require(
"${REPL_DEVHUB}/widget/core.lib.url"
) || { getLinkUsingCurrentGateway: () => {} };

const instance = props.instance ?? "";
const { cacheUrl, contract } = VM.require(`${instance}/widget/config.data`);

const data = props.data;
const onChange = props.onChange ?? (() => {});
const onChangeKeyup = props.onChangeKeyup ?? (() => {}); // in case where we want immediate action
Expand All @@ -30,21 +34,8 @@ const showAccountAutoComplete = props.showAutoComplete ?? false;
const showProposalIdAutoComplete = props.showProposalIdAutoComplete ?? false;
const autoFocus = props.autoFocus ?? false;

const queryName = "${REPL_PROPOSAL_FEED_INDEXER_QUERY_NAME}";
const query = `query GetLatestSnapshot($offset: Int = 0, $limit: Int = 10, $where: ${queryName}_bool_exp = {}) {
${queryName}(
offset: $offset
limit: $limit
order_by: {proposal_id: desc}
where: $where
) {
name
proposal_id
}
}`;

const proposalLink = getLinkUsingCurrentGateway(
`${REPL_DEVHUB}/widget/app?page=proposal&id=`
`${contract}/widget/app?page=proposal&id=`
);

const code = `
Expand Down Expand Up @@ -88,11 +79,11 @@ const code = `
}

.CodeMirror {
min-height:200px !important; // for autocomplete to be visble
min-height:200px !important; // for autocomplete to be visible
}

.CodeMirror-scroll {
min-height:200px !important; // for autocomplete to be visble
min-height:200px !important; // for autocomplete to be visible
}

${embeddCSS}
Expand Down Expand Up @@ -129,7 +120,6 @@ let isEditorInitialized = false;
let followingData = {};
let profilesData = {};
let proposalLink = '';
let query = '';
let showAccountAutoComplete = ${showAccountAutoComplete};
let showProposalIdAutoComplete = ${showProposalIdAutoComplete};

Expand Down Expand Up @@ -183,12 +173,11 @@ function getSuggestedAccounts(term) {
return results;
}

async function asyncFetch(endpoint, { method, headers, body }) {
async function asyncFetch(endpoint, { method, headers }) {
try {
const response = await fetch(endpoint, {
method: method,
headers: headers,
body: body
});

if (!response.ok) {
Expand All @@ -212,46 +201,28 @@ function extractNumbers(str) {
return numbers;
};

function searchCacheApi(searchProposalId) {
let searchInput = encodeURI(searchProposalId);

let searchUrl = "${cacheUrl}/proposals/search/" + searchInput;

return asyncFetch(searchUrl, {
method: "GET",
headers: {
accept: "application/json",
},
}).catch((error) => {
console.log("Error searching cache api", error);
});
}

async function getSuggestedProposals(id) {
let results = [];
const variables = {
limit: 5,
offset: 0,
where: {},
};

if (id) {
const proposalId = extractNumbers(id);
if (proposalId) {
variables["where"] = { proposal_id: { _eq: id } };
} else {
variables["where"] = {
_or: [
{ name: { _iregex: id } },
{ summary: { _iregex: id } },
{ description: { _iregex: id } },
],
};
}
const searchResults = await searchCacheApi(id);
results = searchResults?.records || [];
}
await asyncFetch("https://near-queryapi.api.pagoda.co/v1/graphql", {
method: "POST",
headers: { "x-hasura-role": "${REPL_INDEXER_HASURA_ROLE}" },
body: JSON.stringify({
query: query,
variables: variables,
operationName: "GetLatestSnapshot",
}),
})
.then((res) => {
const proposals =
res?.data?.[
"${REPL_PROPOSAL_FEED_INDEXER_QUERY_NAME}"
];
results = proposals;
})
.catch((error) => {
console.error(error);
});
return results;
};

Expand Down Expand Up @@ -402,7 +373,7 @@ if (showAccountAutoComplete) {
});
});
}
// show dropwdown only when @ is at first place or when there is a space before @
// show dropdown only when @ is at first place or when there is a space before @
if (!mentionToken && (token.string === "@" && cursor.ch === 1 || token.string === "@" && cm.getTokenAt({line:cursor.line, ch: cursor.ch - 1}).string == ' ')) {
mentionToken = token;
mentionCursorStart = cursor;
Expand Down Expand Up @@ -439,6 +410,7 @@ if (showProposalIdAutoComplete) {
let proposalId;
let referenceCursorStart;
const dropdown = document.getElementById("referencedropdown");
// Create loader element once and store it
const loader = document.createElement('div');
loader.className = 'loader';
loader.textContent = 'Loading...';
Expand All @@ -460,8 +432,10 @@ if (showProposalIdAutoComplete) {
const proposalIdInput = cm.getRange(referenceCursorStart, cursor);
dropdown.innerHTML = ''; // Clear previous content
dropdown.appendChild(loader); // Show loader

const suggestedProposals = await getSuggestedProposals(proposalIdInput);

// Clear dropdown including loader
dropdown.innerHTML = suggestedProposals
.map(
(item) =>
Expand Down Expand Up @@ -496,7 +470,7 @@ if (showProposalIdAutoComplete) {
}
}

// show dropwdown only when there is space before # or it's first char
// show dropdown only when there is space before # or it's first char
if (!proposalId && (token.string === "#" && cursor.ch === 1 || token.string === "#" && cm.getTokenAt({line:cursor.line, ch: cursor.ch - 1}).string == ' ')) {
proposalId = token;
referenceCursorStart = cursor;
Expand Down Expand Up @@ -545,9 +519,6 @@ window.addEventListener("message", (event) => {
if (event.data.profilesData) {
profilesData = JSON.parse(event.data.profilesData);
}
if (event.data.query) {
query = event.data.query;
}
if (event.data.proposalLink) {
proposalLink = event.data.proposalLink;
}
Expand All @@ -570,7 +541,6 @@ return (
content: props.data?.content ?? "",
followingData,
profilesData: JSON.stringify(profilesData),
query: query,
handler: props.data.handler,
proposalLink: proposalLink,
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ const ContentEditor = useMemo(() => {
<Widget
src="${REPL_DEVHUB}/widget/devhub.components.molecule.Compose"
props={{
instance: "${REPL_DEVHUB}",
data: content,
onChange: setContent,
height: "250",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ const Compose = useMemo(() => {
<Widget
src={"${REPL_DEVHUB}/widget/devhub.components.molecule.Compose"}
props={{
instance: "${REPL_DEVHUB}",
data: comment,
onChangeKeyup: setComment,
autocompleteEnabled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,7 @@ const DescriptionComponent = useMemo(() => {
<Widget
src={"${REPL_DEVHUB}/widget/devhub.components.molecule.Compose"}
props={{
instance: "${REPL_DEVHUB}",
data: description,
onChange: setDescription,
autocompleteEnabled: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,9 @@ const Compose = ({
{state.selectedTab === "editor" ? (
<>
<Widget
src={"${REPL_EVENTS}/widget/devhub.components.molecule.SimpleMDE"}
src={"${REPL_DEVHUB}/widget/devhub.components.molecule.SimpleMDE"}
props={{
instance: props.instance,
data: { handler: state.handler, content: state.data },
onChange: (content) => {
State.update({ data: content, handler: "update" });
Expand Down
Loading
Loading