Skip to content

Commit

Permalink
Merge pull request #19 from songz/s4
Browse files Browse the repository at this point in the history
updates #1 - creates a dockerfile
  • Loading branch information
songz authored Dec 6, 2020
2 parents d371c40 + e28e54d commit 66f8910
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 56 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ data
public/assets
logs
public/main.js
node_modules
.git
14 changes: 14 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Build image
FROM node:12.18-alpine AS build
WORKDIR /build

# Install only the production dependencies
RUN npm i

COPY . /build

USER node

ENV PORT=3000
EXPOSE 3000
CMD ["npm", "start"]
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"setup": "./scripts/setup.sh",
"start": "node app.js",
"server": "webpack --config webpack.config.js && nodemon app.js",
"dev": "webpack --config webpack.config.js --watch"
},
Expand Down
116 changes: 60 additions & 56 deletions src/search.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
let resultContainer;
let searchBox;
let allNotes;
let resultElements = []
let noteMap = {}
let resultElements = [];
let noteMap = {};

const search = () => {
const searchQuery = searchBox.value
const searchQuery = searchBox.value;
if (searchQuery.length < 2) {
return resultContainer.innerHTML = ''
return (resultContainer.innerHTML = "");
}
fetch(`/search/${searchQuery}`).then( r=> r.text() ).then(data => {
// invalidate old requests
if( searchBox.value !== searchQuery) return
const dataArr = data.split('\n').filter( d => d ).map( d => {
const result = d.substr(5) // get rid of data/ prefix
const resultArr = result.split(':')
const title = resultArr.shift()
const match = resultArr.join(':')
return {
title, match
}
})
resultContainer.innerHTML = ''
const newTitleMap = {...noteMap}
resultElements = dataArr.map( (obj, idx) => {
delete newTitleMap[obj.title.toLowerCase()]
return new Result(obj.title, obj.match, idx)
})
Object.keys(newTitleMap).forEach( title => {
if (!title.includes(searchQuery.toLowerCase())) {
return
}
resultElements.push(
new Result(newTitleMap[title], '', resultElements.length)
)
})
})
}
fetch(`/search/${searchQuery}`)
.then((r) => r.text())
.then((data) => {
// invalidate old requests
if (searchBox.value !== searchQuery) return;
const dataArr = data
.split("\n")
.filter((d) => d)
.map((d) => {
const result = d.replace("./data/", ""); // get rid of ./data/ prefix
const resultArr = result.split(":");
const title = resultArr.shift();
const match = resultArr.join(":");
return {
title,
match,
};
});
resultContainer.innerHTML = "";
const newTitleMap = { ...noteMap };
resultElements = dataArr.map((obj, idx) => {
delete newTitleMap[obj.title.toLowerCase()];
return new Result(obj.title, obj.match, idx);
});
Object.keys(newTitleMap).forEach((title) => {
if (!title.includes(searchQuery.toLowerCase())) {
return;
}
resultElements.push(
new Result(newTitleMap[title], "", resultElements.length)
);
});
});
};

function Result(name, value, idx) {
const div = document.createElement('div')
div.className = 'resultContainer'
const div = document.createElement("div");
div.className = "resultContainer";
if (!idx) {
div.className += ' selected'
div.className += " selected";
}
div.innerHTML = `
<p>
Expand All @@ -51,31 +57,29 @@ function Result(name, value, idx) {
</span>
${value}
</p>
`
div.addEventListener('click', () => {
this.select()
})
`;
div.addEventListener("click", () => {
this.select();
});

resultContainer.append(div)
resultContainer.append(div);
this.select = () => {
window.location = `/notes/${name}`
}
window.location = `/notes/${name}`;
};
}

const initSearch = (titleMap) => {
resultContainer = document.querySelector('#resultListContainer')
searchBox = document.querySelector('#searchBox')
allNotes = document.querySelectorAll('.card-columns .card-title')
searchBox.focus()
noteMap = titleMap
searchBox.addEventListener('keyup', (e) => {
console.log(e.key)
if (e.key === 'Enter') {
return resultElements[0].select()
resultContainer = document.querySelector("#resultListContainer");
searchBox = document.querySelector("#searchBox");
allNotes = document.querySelectorAll(".card-columns .card-title");
searchBox.focus();
noteMap = titleMap;
searchBox.addEventListener("keyup", (e) => {
if (e.key === "Enter") {
return resultElements[0].select();
}
search()
})

}
search();
});
};

module.exports = initSearch
module.exports = initSearch;

0 comments on commit 66f8910

Please sign in to comment.