Merging in the pWiki prototype...

This commit is contained in:
Alex A. Naanou 2016-07-05 03:19:20 +03:00
commit 7269ab0707
3 changed files with 222 additions and 0 deletions

4
ext-lib/jquery.js vendored Executable file

File diff suppressed because one or more lines are too long

83
index.html Executable file
View File

@ -0,0 +1,83 @@
<!DOCTYPE html>
<html>
<style>
</style>
<script src="ext-lib/jquery.js"></script>
<script src="wiki.js"></script>
<script>
var clear = () => {
delete localStorage['wiki-data']
delete localStorage['wiki-location']
}
var reload = () => {
$('.dir').text('/' + Wiki.dir)
$('.title').text(Wiki.title)
$('.text').html(activateWikiWords(Wiki.text))
// XXX save...
localStorage['wiki-data'] = JSON.stringify(Wiki.__wiki_data)
localStorage['wiki-location'] = Wiki.location
}
var go = (path) => {
Wiki.location = path
reload()
}
var clearWikiWords = elem => {
// clear existing...
elem.find('.WikiWord').each(function(){
$(this).replaceWith(this.childNodes)})
return elem
}
var activateWikiWords = text =>
text
// set new...
.replace(
RegExp('('+[
'[A-Z][a-z0-9]+[A-Z][a-z0-9][a-zA-Z0-9]*\/\\w+',
'[A-Z][a-z0-9]+[A-Z][a-z0-9][a-zA-Z0-9]*',
'\\[[^\\]]+\\]',
].join('|') +')', 'g'),
'<a class="WikiWord" href="#" onclick="go($(this).text().replace(/^\\[|\\]$/g, \'\'))">$1</a>')
$(() => {
// load stored data...
Wiki.__wiki_data = localStorage['wiki-data'] ?
JSON.parse(localStorage['wiki-data'])
: data
Wiki.location = localStorage['wiki-location'] || Wiki.location
reload()
$('.title')
.on('keyup', () => { Wiki.title = $('.title').text() })
// live update text...
// XXX is this the right way to go for large documents???
$('.text')
.on('keyup', () => {
Wiki.text = clearWikiWords($('.text').clone()).html() })
// XXX do this live, but on a timeout after user input...
// XXX need to place the cursor in the same position...
.blur(() => { reload() })
})
</script>
<body>
<div class="dir"></div>
<hr>
<h1 class="title" contenteditable></h1>
<div class="text" contenteditable></div>
</body>
</html>

135
wiki.js Executable file
View File

@ -0,0 +1,135 @@
/**********************************************************************
*
*
*
**********************************************************************/
//var DEBUG = DEBUG != null ? DEBUG : true
/*********************************************************************/
// data store...
// Format:
// {
// <path>: {
// text: <text>,
//
// links: [
// <offset>: <link>,
// ],
// }
// }
var data = {
'/': {
text: 'base page...'
},
'dir/page': {
text: 'some text...'
},
}
var path2lst = path =>
(path instanceof Array ?
path
: path.split(/[\\\/]+/g))
.map(p => p.trim())
.filter(p =>
p != '' && p != '.')
var normalizePath = path =>
path2lst(path).join('/')
var Wiki = {
__wiki_data: data,
// current location...
get location(){
return this.__location || '/' },
set location(value){
this.__location = normalizePath(value) },
// page path...
//
// Format:
// <dir>/<title>
//
// NOTE: changing this will move the page to the new path and change
// .location acordingly...
// NOTE: same applies to path parts below...
get path(){
return this.location },
set path(value){
var l = this.location
value = normalizePath(value)
this.__wiki_data[value] = this.__wiki_data[l] || {}
this.location = value
delete this.__wiki_data[l]
},
// path parts: directory...
//
// NOTE: see .path for details...
get dir(){
return path2lst(this.location).slice(0, -1).join('/') },
set dir(value){
this.path = value +'/'+ this.title },
// path parts: title...
//
// NOTE: see .path for details...
get title(){
return path2lst(this.location).pop() },
set title(value){
this.path = this.dir +'/'+ value },
// page content...
get text(){
return (this.__wiki_data[this.location] || {}).text || ''
},
set text(value){
var l = this.location
this.__wiki_data[l] = this.__wiki_data[l] || {}
this.__wiki_data[l].text = value
},
// XXX
get links(){
},
set links(value){
},
// XXX
get list(){
},
set list(value){
},
// serialization...
get json(){
return {
path: this.path,
text: this.text,
}
},
set json(value){
if(value.title){
this.title = value.title
}
if(value.text){
this.text = value.text
}
},
}
/**********************************************************************
* vim:set ts=4 sw=4 : */