From 60d25c22542160845cd025cfa4368c61963659d3 Mon Sep 17 00:00:00 2001 From: Alon Levy Date: Sun, 8 Mar 2015 14:24:38 +0200 Subject: client/textanalysis: add element_at_position__is_link --- src/client/textanalysis.js | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) (limited to 'src/client') diff --git a/src/client/textanalysis.js b/src/client/textanalysis.js index d44dfd06..64d7af7c 100644 --- a/src/client/textanalysis.js +++ b/src/client/textanalysis.js @@ -20,6 +20,10 @@ util.assert(nodetypes.indexOf(default_nodetype) !== -1); var _get_lastnode, get_lastnode = function (editgraph, cursor) { return undefined !== _get_lastnode ? _get_lastnode(editgraph, cursor) : null; + }, + _element_at_position__is_link, + element_at_position__is_link = function(cursor) { + return undefined !== _element_at_position__is_link ? _element_at_position__is_link(cursor) : false; }; var sugg_name = {}, @@ -51,13 +55,22 @@ function even_second(_, i) } function list_first_pred(list, default_, pred) +{ + var index = list_first_pred__index(list, pred); + + if (undefined === index) { + return default_; + } + return list[index]; +} + +function list_first_pred__index(list, pred) { for (var i = 0 ; i < list.length ; ++i) { if (pred(list[i])) { - return list[i]; + return i; } } - return default_; } function list_product(l1, l2) @@ -597,9 +610,20 @@ var textAnalyser = function (spec) { return node; } + function __element_at_position__is_link(cursor) { + var index = list_first_pred__index(thirds, function (d) { + return cursor >= d.start && cursor < d.end; + }); + index = undefined === index ? thirds.length - 1 : index; + return index % 2 == 1; + } + + // FIXME: ugh. + _get_lastnode = finalize || tokens.length === 0 ? function () { return null; } : lookup_node_in_bounds; + _element_at_position__is_link = __element_at_position__is_link; return ret; }; @@ -617,6 +641,8 @@ return { ANALYSIS_NODE: ANALYSIS_NODE, ANALYSIS_LINK:ANALYSIS_LINK, + element_at_position__is_link: element_at_position__is_link, + //for the external arrow-type changer lastnode: get_lastnode, set_type: function(name, nodetype) { -- cgit v1.3.1