Skip to content

Commit

Permalink
Use absolute path for dragText with parent #2003
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico Hoffmann authored and bastianallgeier committed Sep 16, 2019
1 parent 9a25f40 commit 2256afd
Show file tree
Hide file tree
Showing 10 changed files with 138 additions and 24 deletions.
2 changes: 1 addition & 1 deletion config/fields/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@
$field = $this->field();
$uploads = $field->uploads();

return $field->upload($this, $uploads, function ($file) use ($field) {
return $field->upload($this, $uploads, function ($file, $parent) use ($field) {
return $file->panelPickerData([
'image' => $field->image(),
'info' => $field->info(),
Expand Down
2 changes: 1 addition & 1 deletion config/fields/mixins/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
throw new Exception('The file could not be uploaded');
}

return $map($file);
return $map($file, $parent);
});
}
]
Expand Down
9 changes: 7 additions & 2 deletions config/fields/textarea.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,15 @@
[
'pattern' => 'upload',
'action' => function () {
return $this->field()->upload($this, $this->field()->uploads(), function ($file) {
$field = $this->field();
$uploads = $field->uploads();

return $this->field()->upload($this, $uploads, function ($file, $parent) use ($field) {
$absolute = $field->model()->is($parent) === false;

return [
'filename' => $file->filename(),
'dragText' => $file->dragText(),
'dragText' => $file->dragText($absolute),
];
});
}
Expand Down
5 changes: 1 addition & 4 deletions config/sections/files.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@

return null;
},
'dragTextType' => function () {
return (option('panel.kirbytext') ?? true) ? 'kirbytext' : 'markdown';
},
'parent' => function () {
return $this->parentModel();
},
Expand Down Expand Up @@ -104,7 +101,7 @@
$image = $file->panelImage($this->image);

$data[] = [
'dragText' => $file->dragText($this->dragTextType, $dragTextAbsolute),
'dragText' => $file->dragText($dragTextAbsolute),
'filename' => $file->filename(),
'id' => $file->id(),
'text' => $file->toString($this->text),
Expand Down
5 changes: 1 addition & 4 deletions config/sections/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@
}
],
'computed' => [
'dragTextType' => function () {
return option('panel.kirbytext', true) ? 'kirbytext' : 'markdown';
},
'parent' => function () {
return $this->parentModel();
},
Expand Down Expand Up @@ -144,7 +141,7 @@

$data[] = [
'id' => $item->id(),
'dragText' => $item->dragText($this->dragTextType),
'dragText' => $item->dragText(),
'text' => $item->toString($this->text),
'info' => $item->toString($this->info ?? false),
'parent' => $item->parentId(),
Expand Down
6 changes: 3 additions & 3 deletions src/Cms/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ public function contentFileName(): string
* gets dragged onto a textarea
*
* @internal
* @param string $type
* @param bool $absolute
* @return string
*/
public function dragText($type = 'kirbytext', bool $absolute = false): string
public function dragText(bool $absolute = false): string
{
$url = $absolute ? $this->id() : $this->filename();
$type = option('panel.kirbytext', true) ? 'kirbytext' : 'markdown';
$url = $absolute ? $this->id() : $this->filename();

switch ($type) {
case 'kirbytext':
Expand Down
4 changes: 3 additions & 1 deletion src/Cms/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ public function diruri(): string
* @internal
* @return string
*/
public function dragText($type = 'kirbytext'): string
public function dragText(): string
{
$type = option('panel.kirbytext', true) ? 'kirbytext' : 'markdown';

switch ($type) {
case 'kirbytext':
return '(link: ' . $this->id() . ' text: ' . $this->title() . ')';
Expand Down
62 changes: 58 additions & 4 deletions tests/Cms/Files/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,36 @@ public function testDragText()
]);

$file = $page->file('test.pdf');

$this->assertEquals('(file: test.pdf)', $file->dragText());
$this->assertEquals('[test.pdf](test.pdf)', $file->dragText('markdown'));
}

public function testDragTextMarkdown()
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'options' => [
'panel' => [
'kirbytext' => false
]
],
'site' => [
'children' => [
[
'slug' => 'test',
'files' => [
[
'filename' => 'test.pdf'
]
]
]
]
]
]);

$file = $app->page('test')->file('test.pdf');
$this->assertEquals('[test.pdf](test.pdf)', $file->dragText());
}

public function testDragTextForImages()
Expand All @@ -79,9 +106,36 @@ public function testDragTextForImages()
]);

$file = $page->file('test.jpg');

$this->assertEquals('(image: test.jpg)', $file->dragText());
$this->assertEquals('![](test.jpg)', $file->dragText('markdown'));
}

public function testDragTextForImagesMarkdown()
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'options' => [
'panel' => [
'kirbytext' => false
]
],
'site' => [
'children' => [
[
'slug' => 'test',
'files' => [
[
'filename' => 'test.jpg'
]
]
]
]
]
]);

$file = $app->page('test')->file('test.jpg');
$this->assertEquals('![](test.jpg)', $file->dragText());
}

public function testFilename()
Expand Down
53 changes: 51 additions & 2 deletions tests/Cms/Pages/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,30 @@ public function testDragText()
]);

$this->assertEquals('(link: test text: test)', $page->dragText());
$this->assertEquals('[test](/test)', $page->dragText('markdown'));
}

public function testDragTextMarkdown()
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'options' => [
'panel' => [
'kirbytext' => false
]
],
'site' => [
'children' => [
[
'slug' => 'test'
]
]
]
]);

$page = $app->page('test');
$this->assertEquals('[test](/test)', $page->dragText());
}

public function testDragTextWithTitle()
Expand All @@ -142,7 +165,33 @@ public function testDragTextWithTitle()
]);

$this->assertEquals('(link: test text: Test Title)', $page->dragText());
$this->assertEquals('[Test Title](/test)', $page->dragText('markdown'));
}

public function testDragTextWithTitleMarkdown()
{
$app = new App([
'roots' => [
'index' => '/dev/null'
],
'options' => [
'panel' => [
'kirbytext' => false
]
],
'site' => [
'children' => [
[
'slug' => 'test',
'content' => [
'title' => 'Test Title'
]
]
]
]
]);

$page = $app->page('test');
$this->assertEquals('[Test Title](/test)', $page->dragText());
}

public function testId()
Expand Down
14 changes: 12 additions & 2 deletions tests/Form/Fields/TextareaFieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Kirby\Form\Fields;

use Kirby\Form\Field;

class TextareaFieldTest extends TestCase
{
public function testDefaultProps()
Expand Down Expand Up @@ -129,6 +127,18 @@ public function testUploadsDisabled()
$this->assertFalse($field->uploads());
}

public function testUploadsParent()
{
$field = $this->field('textarea', [
'value' => 'test',
'uploads' => [
'parent' => 'page.parent'
]
]);

$this->assertEquals(['parent' => 'page.parent', 'accept' => '*'], $field->uploads());
}

public function testUploadsTemplate()
{
$field = $this->field('textarea', [
Expand Down

0 comments on commit 2256afd

Please sign in to comment.