mirror of
https://github.com/YGGverse/Yoda.git
synced 2026-04-01 17:15:28 +00:00
implement link open in the new tab on middle button click
This commit is contained in:
parent
3186ac6846
commit
619cc9cba8
2 changed files with 101 additions and 41 deletions
|
|
@ -9,6 +9,8 @@ use \GtkLabel;
|
||||||
|
|
||||||
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content;
|
use \Yggverse\Yoda\Entity\Browser\Container\Page\Content;
|
||||||
|
|
||||||
|
use \Yggverse\Net\Address;
|
||||||
|
|
||||||
abstract class Markup
|
abstract class Markup
|
||||||
{
|
{
|
||||||
public GtkLabel $gtk;
|
public GtkLabel $gtk;
|
||||||
|
|
@ -121,4 +123,80 @@ abstract class Markup
|
||||||
abstract public function set(
|
abstract public function set(
|
||||||
string $value
|
string $value
|
||||||
): void;
|
): void;
|
||||||
|
|
||||||
|
// Tools
|
||||||
|
protected function _line(
|
||||||
|
int $offset
|
||||||
|
): ?string
|
||||||
|
{
|
||||||
|
if (is_null($this->_source))
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$start = strrpos(
|
||||||
|
substr(
|
||||||
|
$this->_source,
|
||||||
|
0,
|
||||||
|
$offset
|
||||||
|
),
|
||||||
|
PHP_EOL
|
||||||
|
) + 1;
|
||||||
|
|
||||||
|
$end = strpos(
|
||||||
|
$this->_source,
|
||||||
|
PHP_EOL,
|
||||||
|
$offset
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($end === false)
|
||||||
|
{
|
||||||
|
$end = strlen(
|
||||||
|
$this->_source
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return substr(
|
||||||
|
$this->_source,
|
||||||
|
$start,
|
||||||
|
$end - $start
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _wrap(
|
||||||
|
string $source
|
||||||
|
): string
|
||||||
|
{
|
||||||
|
if ($wrap = $this->_wrap ? $this->_wrap : $this::WRAP)
|
||||||
|
{
|
||||||
|
return wordwrap(
|
||||||
|
$source,
|
||||||
|
$wrap,
|
||||||
|
PHP_EOL,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Exception;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function _url(
|
||||||
|
string $link
|
||||||
|
): ?string
|
||||||
|
{
|
||||||
|
$address = new Address(
|
||||||
|
$link
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($address->isRelative())
|
||||||
|
{
|
||||||
|
$address->toAbsolute(
|
||||||
|
new Address(
|
||||||
|
$this->content->page->navbar->request->getValue()
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $address->get();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,7 +20,8 @@ use \Yggverse\Gemtext\Entity\Listing;
|
||||||
use \Yggverse\Gemtext\Entity\Quote;
|
use \Yggverse\Gemtext\Entity\Quote;
|
||||||
use \Yggverse\Gemtext\Entity\Text;
|
use \Yggverse\Gemtext\Entity\Text;
|
||||||
|
|
||||||
use \Yggverse\Net\Address;
|
use \Yggverse\Gemtext\Parser\Link as LinkParser;
|
||||||
|
|
||||||
|
|
||||||
class Gemtext extends Markup
|
class Gemtext extends Markup
|
||||||
{
|
{
|
||||||
|
|
@ -287,18 +288,36 @@ class Gemtext extends Markup
|
||||||
GdkEvent $event
|
GdkEvent $event
|
||||||
): bool
|
): bool
|
||||||
{
|
{
|
||||||
// Open link in new tab on middle button click
|
// Open link in the new tab on middle button click
|
||||||
if ($event->button->button == Gdk::BUTTON_MIDDLE)
|
if ($event->button->button == Gdk::BUTTON_MIDDLE)
|
||||||
{
|
{
|
||||||
|
// Detect cursor position
|
||||||
$result = $label->get_layout()->xy_to_index(
|
$result = $label->get_layout()->xy_to_index(
|
||||||
$event->button->x * Pango::SCALE,
|
$event->button->x * Pango::SCALE,
|
||||||
$event->button->y * Pango::SCALE
|
$event->button->y * Pango::SCALE
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Position detected
|
||||||
if ($result)
|
if ($result)
|
||||||
{
|
{
|
||||||
// @TODO
|
// Get entire line from source
|
||||||
return true;
|
if ($line = $this->_line($result['index_']))
|
||||||
|
{
|
||||||
|
// Parse gemtext href
|
||||||
|
if ($href = LinkParser::getAddress($line))
|
||||||
|
{
|
||||||
|
// Format URL
|
||||||
|
if ($url = $this->_url($href))
|
||||||
|
{
|
||||||
|
// Open
|
||||||
|
$this->content->page->container->tab->append(
|
||||||
|
$url
|
||||||
|
);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -313,41 +332,4 @@ class Gemtext extends Markup
|
||||||
// @TODO
|
// @TODO
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function _wrap(
|
|
||||||
string $source
|
|
||||||
): string
|
|
||||||
{
|
|
||||||
if ($wrap = $this->_wrap ? $this->_wrap : $this::WRAP)
|
|
||||||
{
|
|
||||||
return wordwrap(
|
|
||||||
$source,
|
|
||||||
$wrap,
|
|
||||||
PHP_EOL,
|
|
||||||
false
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception;
|
|
||||||
}
|
|
||||||
|
|
||||||
private function _url(
|
|
||||||
string $link
|
|
||||||
): ?string
|
|
||||||
{
|
|
||||||
$address = new Address(
|
|
||||||
$link
|
|
||||||
);
|
|
||||||
|
|
||||||
if ($address->isRelative())
|
|
||||||
{
|
|
||||||
$address->toAbsolute(
|
|
||||||
new Address(
|
|
||||||
$this->content->page->navbar->request->getValue()
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
return $address->get();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue