Translate metadata to field content

hh hh at
Tue Feb 18 09:40:42 EST 2020

> JLG wrote:
> I'm still working on htmltext in a field. There are anchors
> that indicate positions elsewhere in the file. A browser will
> automatically find and scroll to the right place in the text.
> I need to reproduce that.

Besides using style runs you could try the following simple
method that works with the htmltext of a (locked) field and
works also in a browser.

-- Assuming (as usual)
-- [1] you use the following style for a link target
--     <a name="target10">X</a>
--     where X is any char e.g. a space (else LC removes
--     the tag in its htmltext!)
-- [2] you use the following style for a local page link
--     <a href="#target10">Target10</a>

Then the following in the field's script does what you want.

-- jumps to local page links (as described above) ##[-hh 2020]
on linkClicked pUrl
  if pUrl begins with "#" then
    delete char 1 of pUrl
    put "<a name=" &quote& pUrl &quote& ">" into tTarget
    -- of course there should be no return in tTarget
    put the htmltext of me into tHtml
    if tTarget is in tHtml then 
      set itemdelimiter to tTarget
      put numToChar(1) after item 1 of tHtml
      set htmltext of me to tHtml
      set itemdelimiter to numToChar(1)
      put 1+length(item 1 of me) into tL
      delete char tL of me
      -- select char tL to tL-1 of me --> variant
      select char tL of me --> see it in locked field
      -- scrollSelectionIntoView --> optional
    end if
  end if
end linkClicked

-- works for any selection in a field ##[-hh 2020]
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