diff --git a/code/site/components/com_pages/template/filter/toc.php b/code/site/components/com_pages/template/filter/toc.php index d1f25095..093c5969 100644 --- a/code/site/components/com_pages/template/filter/toc.php +++ b/code/site/components/com_pages/template/filter/toc.php @@ -12,20 +12,10 @@ class ComPagesTemplateFilterToc extends ComPagesTemplateFilterAbstract protected function _initialize(KObjectConfig $config) { $config->append(array( - 'min_level' => 1, + 'min_level' => 2, 'max_level' => 6, - 'anchor' => [ - 'enabled' => true, - 'options' => [ - 'placement' => 'right', - 'visibale' => 'hover', - 'icon' => "", - 'class' => null, - 'truncate' => null, - 'arialabel' => 'Anchor', - ], - 'selector' => 'article h2, article h3, article h4, article h5, article h6', - ], + 'anchor' => true, + 'icon' => '#' )); parent::_initialize($config); @@ -46,37 +36,71 @@ public function filter(&$text) */ $matches = array(); - if($this->isEnabled() && preg_match_all('#]*>(.+?)#is', $text, $headers)) + if($this->isEnabled()) { + preg_match_all('#]*>(.+?)#is', $text, $headers); + foreach($headers[1] as $key => $level) { $content = $headers[2][$key]; $id = $this->_generateId($content); - $result = ''.$content.''; + $result = ''.$content.''; $text = str_replace($headers[0][$key], $result, $text); - if($this->getConfig()->anchor->enabled) { - $text .= $this->getTemplate()->helper('behavior.anchor', KObjectConfig::unbox($this->getConfig()->anchor)); + if($this->getConfig()->anchor) + { + $icon = $this->getConfig()->icon; + + if(strlen($icon) == 1) { + $icon = '"'.$icon.'"'; + } + + //Accessible anchor links, see https://codepen.io/johanjanssens/pen/PoWObpL + $text .= << +@media (hover: hover) { + .toc-anchor { + margin-left: -1em; + padding-left: 1em; + } + .toc-anchor a { + text-decoration: none; + pointer-events: none; + color: inherit !important; + } + .toc-anchor a::after { + content: $icon; + font-size: 0.8em; + float: left; + padding: 0 .15em; /* to make the content a bigger target */ + pointer-events: auto; + margin-left: -1em; + visibility: hidden; + display: inline-block; + } + .toc-anchor:hover a::after { + visibility: visible; + } +} + +ANCHOR; + } } - } - /* - * Table of content - */ - $matches = array(); - if(preg_match_all('#]*)>#siU', $text, $matches)) - { - foreach($matches[0] as $key => $match) + /* + * Table of content + */ + $matches = array(); + if(preg_match_all('#]*)>#siU', $text, $matches)) { - $toc = ''; - if($this->isEnabled()) + foreach($matches[0] as $key => $match) { + $toc = ''; $attributes = array_merge($attributes, $this->parseAttributes($matches[1][$key])); - $headers = array(); - if(preg_match_all('#]*>(.+?)#is', $text, $headers)) + if($headers) { $toc = '
    '; diff --git a/code/site/components/com_pages/template/helper/behavior.php b/code/site/components/com_pages/template/helper/behavior.php index 061ed9cd..e140f2dd 100644 --- a/code/site/components/com_pages/template/helper/behavior.php +++ b/code/site/components/com_pages/template/helper/behavior.php @@ -9,44 +9,6 @@ class ComPagesTemplateHelperBehavior extends ComKoowaTemplateHelperBehavior { - public function anchor($config = array()) - { - $config = new ComPagesObjectConfig($config); - $config->append(array( - 'debug' => $this->getConfig('pages.config')->debug, - 'options' => array( - 'placement' => 'right', - 'visibale' => 'hover', - 'icon' => "", - 'class' => null, - 'truncate' => null, - 'arialabel' => 'Anchor', - ), - 'version' => '4.3.0', - 'selector' => 'article h2, article h3, article h4, article h5, article h6', - )); - - $html = ''; - if (!static::isLoaded('anchor')) - { - $selector = json_encode($config->selector); - - $html .= ''; - $html .= << -document.addEventListener("DOMContentLoaded", function(event) { - anchors.options = $config->options - anchors.add($selector);if(document.querySelector('.no-anchor')!==null){anchors.remove('.no-anchor');} -}) - -ANCHOR; - - static::setLoaded('anchor'); - } - - return $html; - } - public function prefetcher($config = array()) { $config = new ComPagesObjectConfig($config);