Improve link search handling
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Peter Stuifzand 2020-06-05 22:53:03 +02:00
parent e1254ba3b4
commit 2b43fd9ea9
5 changed files with 1318 additions and 227 deletions

1457
editor/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,12 @@
{ {
"devDependencies": { "devDependencies": {
"clean-webpack-plugin": "^3.0.0", "clean-webpack-plugin": "^3.0.0",
"esm": "^3.2.25",
"html-webpack-plugin": "^3.2.0", "html-webpack-plugin": "^3.2.0",
"mocha": "^7.2.0",
"mocha-webpack": "^1.1.0",
"scss-loader": "0.0.1", "scss-loader": "0.0.1",
"webpack": "^4.39.2", "webpack": "^4.43.0",
"webpack-cli": "^3.3.7", "webpack-cli": "^3.3.7",
"webpack-dev-server": "^3.11.0" "webpack-dev-server": "^3.11.0"
}, },

View File

@ -4,6 +4,7 @@ import qs from 'querystring'
import $ from 'jquery'; import $ from 'jquery';
import search from './search'; import search from './search';
import createPageSearch from './fuse'; import createPageSearch from './fuse';
import util from './util';
import Mustache from 'mustache'; import Mustache from 'mustache';
import 'jquery-contextmenu'; import 'jquery-contextmenu';
import getCaretCoordinates from './caret-position' import getCaretCoordinates from './caret-position'
@ -261,19 +262,10 @@ if (holder) {
let value = input.value let value = input.value
let end = input.selectionEnd let end = input.selectionEnd
let insideLink = true let [start, insideLink] = util.cursorInsideLink(value, end)
console.log(value, end, start, insideLink)
let insideSearch = false let insideSearch = false
let start = value.lastIndexOf("[[", end)
if (start < 0) {
insideLink = false
}
let linkEnd = value.lastIndexOf("]]", end - 1)
if (insideLink && start < linkEnd) {
insideLink = false
}
if (!insideLink) { if (!insideLink) {
start = value.lastIndexOf("/", end) start = value.lastIndexOf("/", end)
insideSearch = start >= 0 insideSearch = start >= 0
@ -287,6 +279,8 @@ if (holder) {
let query = value.substring(start + 2, end); let query = value.substring(start + 2, end);
showSearchResults(query => titleSearch.search(query), query, input, value, 'link'); showSearchResults(query => titleSearch.search(query), query, input, value, 'link');
return true return true
} else {
$('#link-complete').fadeOut();
} }
}) })
}) })

21
editor/src/util.js Normal file
View File

@ -0,0 +1,21 @@
function cursorInsideLink(value, pos) {
let start = value.lastIndexOf("[[", pos)
if (start < 0) {
return [null, false];
}
if (start === pos) {
return [null, false];
}
if (start === pos-1) {
return [null, false];
}
let linkEnd = value.lastIndexOf("]]", pos-1)
return [start+2, start >= linkEnd]
}
export default {
cursorInsideLink
}

46
editor/test/link.test.js Normal file
View File

@ -0,0 +1,46 @@
// let assert = require('assert');
// let util = require('../src/util')
import assert from 'assert'
import util from '../src/util'
describe('Link', function () {
describe('#cursorInsideLink', function () {
it('should return true when inside link', function () {
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 2), [2, true]);
})
it('should return true when inside link position 1', function () {
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 3), [2,true]);
})
it('should return true when inside link position 2', function () {
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 4), [2,true]);
})
it('should return true when inside link position 3', function () {
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 5), [2,true]);
})
it('should return true when inside link position 4', function () {
assert.deepStrictEqual(util.cursorInsideLink("[[test]]", 6), [2,true]);
})
it('should return false on empty string', function () {
assert.strictEqual(util.cursorInsideLink("", 0)[1], false);
});
it('should return false before open link inside whitespace', function () {
assert.strictEqual(util.cursorInsideLink(" [[", 0)[1], false);
})
it('should return false before one space and open link', function () {
assert.strictEqual(util.cursorInsideLink(" [[", 0)[1], false);
})
it('should return false when before closed link', function () {
assert.strictEqual(util.cursorInsideLink("[[test]]", 0)[1], false);
})
it('should return false when on closed link start tag', function () {
assert.strictEqual(util.cursorInsideLink("[[test]]", 1)[1], false);
})
it('should return false when on link end tag', function () {
assert.strictEqual(util.cursorInsideLink("[[test]]", 7)[1], false);
})
it('should return false when after link end tag', function () {
assert.strictEqual(util.cursorInsideLink("[[test]]", 7)[1], false);
})
})
})