Translate metadata to field content

hh hh at
Thu Feb 20 16:12:35 EST 2020

As others try to optimize ("ping") I'll try an improvement too ("pong")
with using another method that requires to change your link targets ONCE:

Instead of unique targets <a name="target10"> write in your field

"<p hidden>"&numTochar(1)&"target10"&numTochar(1)&"</p>""

Handler replaceTargets below does it (slowly) but you probably don't
need it on mobile.

So assuming (unusual)

-- [1] you use the following for a link target
--     <p hidden>target10<p>
-- [2] you use the following for a local page link
--     <a href="#target10">Target10</a>
-- [3] you don't use <p hidden> elsewhere. Else add
--     an additional marker to it to differentiate.

Then script your field with the following simple handler:

on linkClicked pUrl
  put the milliseconds into m1
  lock messages; lock screen
  if pUrl begins with "#" then
    put numToChar(1)& (char 2 to -1 of pUrl) &numToChar(1) into tTarget
    put 1+offset(tTarget,me)+length(tTarget) into tOff
    select char tOff to tOff+3 of me -- see it in locked field
    -- select char tOff of me -- variant
    scrollSelectionIntoView -- optional
  end if
  put the millisecs -m1 into fld "timing1"
end linkClicked

---- helpers (optionally needed)

-- Note. LC adds also an additional "<p hidden></p>", we don't mind.
on replaceTargets -- should be optimized if used often
  put the millisecs into m1
  lock messages; lock screen
  put the htmltext of fld 1 into tHTML
  set linedel to "<a name="&quote
  set itemdel to "</a>"
  put line 1 of tHtml into tI2
  put numToChar(1) into b1
  repeat for each line L in (line 2 to -1 of tHtml)
    put "<p hidden>"&b1&char 1 to offset(quote,L)-1 of L into item 1 of L
    put offset("</a>",L) into o1
    put b1&"</p>" into char o1 to o1+3 of L
    put L after tI2
  end repeat
  set htmlText of fld 1 to tI2 -- LC translates numToChar(1) to ""
  put the millisecs-m1 into fld "timing2"
end mouseUp

on scrollSelectionIntoView
  put the selectedLoc into tSL
  put the vscroll of me into tV
  put item 2 of tSL - the top of me into tDiff
  if tDiff > 0.75*the height of me then
    set vscroll of me to tV + 0.4*the height of me
  else if tDiff < 0.25*the height of me then
    set vscroll of me to tV - 0.4*the height of me
  end if
end scrollSelectionIntoView

More information about the use-livecode mailing list