Package development

# Основы

Основой MoonShine являются пакеты Laravel. Если вы новичок в разработке пакетов для Laravel, вот несколько ресурсов, которые помогут вам понять основные концепции:

# ServiceProvider

Через ServiceProvider Вашего пакета вы можете добавлять автоматически ресурсы, страницы, формировать меню и правила авторизации и многое другое.

namespace Author\MoonShineMyPackage;
 
use Illuminate\Support\ServiceProvider;
 
class MyPackageServiceProvider extends ServiceProvider
{
public function boot(): void
{
moonshine()
->resources([
new MyPackageResource(),
])
->pages([
new MyPackagePage(),
])
->vendorsMenu([
MenuItem::make('MyPage', new MyPackagePage())
]);
}
}

Также вы можете взаимодействовать с AssetManager или ColorManager

public function boot(): void
{
moonshineAssets()->add([
'path_to_file.css',
'path_to_file.js',
]);
}
public function boot(): void
{
moonshineColors()
->background('#A3C3D9')
->content('#A3C3D9')
->tableRow('#AE76A6')
->dividers('#AE76A6')
->borders('#AE76A6')
->buttons('#AE76A6')
->primary('#CCD6EB')
->secondary('#AE76A6');
}

Если необходимо добавить дополнительную логику авторизации в приложении или во внешнем пакете, то воспользуйтесь методом defineAuthorization.

public function boot(): void
{
moonshine()->defineAuthorization(
static function (ResourceContract $resource, Model $user, string $ability): bool {
return true;
}
);
}

Не забудьте автоматически подключить Ваш ServiceProvider в composer.json

"extra": {
"laravel": {
"providers": [
"Author\\MoonShineMyPackage\\MyPackageServiceProvider"
]
}
}

# Custom field example

Рассмотрим небольшой пример создания собственного поля! Это будет визуальный редактор на основе js плагина CKEditor

Создадим поле с помощью команды moonshine:field и выберем что оно расширяет Textarea

php artisan moonshine:field CKEditor

Уберем лишние методы и добавим css

namespace App\MoonShine\Fields;
 
use MoonShine\Fields\Textarea;
use Closure;
 
class CKEditor extends Textarea
{
protected string $view = 'admin.fields.c-k-editor';
 
protected array $assets = [
'https://cdn.ckeditor.com/ckeditor5/35.3.0/super-build/ckeditor.js'
];
}

Также изменим view поля, реализуем js прямо в blade, но лучшим решением будет вынести в отдельный js файл

<x-moonshine::form.textarea
:attributes="$element->attributes()->merge([
'name' => $element->name()
])->except('x-bind:id')"
x-init="initCkeditor($el)"
>{!! $element->value() ?? '' !!}</x-moonshine::form.textarea>
 
<script>
function initCkeditor(el) {
CKEDITOR.ClassicEditor.create(el, {
// https://ckeditor.com/docs/ckeditor5/latest/features/toolbar/toolbar.html#extended-toolbar-configuration-format
toolbar: {
items: [
'heading', '|',
'bold', 'italic', 'strikethrough', 'underline', 'code', 'subscript', 'superscript', 'removeFormat', '|',
'bulletedList', 'numberedList', 'todoList',
'outdent', 'indent', '|',
'undo', 'redo',
'-',
'exportPDF','exportWord', '|',
'findAndReplace', '|',
'fontSize', 'fontColor', 'fontBackgroundColor',
'alignment', '|',
'link', 'insertImage', 'blockQuote', 'insertTable', 'mediaEmbed', 'codeBlock', 'htmlEmbed', '|',
'specialCharacters', 'horizontalLine', 'sourceEditing'
],
shouldNotGroupWhenFull: true
},
// Changing the language of the interface requires loading the language file using the <script> tag.
// language: 'es',
list: {
properties: {
styles: true,
startIndex: true,
reversed: true
}
},
// https://ckeditor.com/docs/ckeditor5/latest/features/headings.html#configuration
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
{ model: 'heading3', view: 'h3', title: 'Heading 3', class: 'ck-heading_heading3' },
{ model: 'heading4', view: 'h4', title: 'Heading 4', class: 'ck-heading_heading4' },
{ model: 'heading5', view: 'h5', title: 'Heading 5', class: 'ck-heading_heading5' },
{ model: 'heading6', view: 'h6', title: 'Heading 6', class: 'ck-heading_heading6' }
]
},
// https://ckeditor.com/docs/ckeditor5/latest/features/editor-placeholder.html#using-the-editor-configuration
placeholder: 'Welcome to CKEditor 5!',
// https://ckeditor.com/docs/ckeditor5/latest/features/font.html#configuring-the-font-family-feature
fontFamily: {
options: [
'default',
'Arial, Helvetica, sans-serif',
'Courier New, Courier, monospace',
'Georgia, serif',
'Lucida Sans Unicode, Lucida Grande, sans-serif',
'Tahoma, Geneva, sans-serif',
'Times New Roman, Times, serif',
'Trebuchet MS, Helvetica, sans-serif',
'Verdana, Geneva, sans-serif'
],
supportAllValues: true
},
// https://ckeditor.com/docs/ckeditor5/latest/features/font.html#configuring-the-font-size-feature
fontSize: {
options: [ 10, 12, 14, 'default', 18, 20, 22 ],
supportAllValues: true
},
// Be careful with the setting below. It instructs CKEditor to accept ALL HTML markup.
// https://ckeditor.com/docs/ckeditor5/latest/features/general-html-support.html#enabling-all-html-features
htmlSupport: {
allow: [
{
name: /.*/,
attributes: true,
classes: true,
styles: true
}
]
},
codeBlock: {
languages: [
{ language: 'php', label: 'Php' },
{ language: 'js', label: 'Js' }
]
},
// Be careful with enabling previews
// https://ckeditor.com/docs/ckeditor5/latest/features/html-embed.html#content-previews
htmlEmbed: {
showPreviews: true
},
// https://ckeditor.com/docs/ckeditor5/latest/features/link.html#custom-link-attributes-decorators
link: {
decorators: {
addTargetToExternalLinks: true,
defaultProtocol: 'https://',
toggleDownloadable: {
mode: 'manual',
label: 'Downloadable',
attributes: {
download: 'file'
}
}
}
},
// https://ckeditor.com/docs/ckeditor5/latest/features/mentions.html#configuration
mention: {
feeds: [
{
marker: '@',
feed: [
'@apple', '@bears', '@brownie', '@cake', '@cake', '@candy', '@canes', '@chocolate', '@cookie', '@cotton', '@cream',
'@cupcake', '@danish', '@donut', '@dragée', '@fruitcake', '@gingerbread', '@gummi', '@ice', '@jelly-o',
'@liquorice', '@macaroon', '@marzipan', '@oat', '@pie', '@plum', '@pudding', '@sesame', '@snaps', '@soufflé',
'@sugar', '@sweet', '@topping', '@wafer'
],
minimumCharacters: 1
}
]
},
simpleUpload: {
// The URL that the images are uploaded to.
uploadUrl: '{{ route('moonshine.attachments') }}',
},
// The "super-build" contains more premium features that require additional configuration, disable them below.
// Do not turn them on unless you read the documentation and know how to configure them and setup the editor.
removePlugins: [
// These two are commercial, but you can try them out without registering to a trial.
// 'ExportPdf',
// 'ExportWord',
'CKBox',
'CKFinder',
//'EasyImage',
// This sample uses the Base64UploadAdapter to handle image uploads as it requires no configuration.
// https://ckeditor.com/docs/ckeditor5/latest/features/images/image-upload/base64-upload-adapter.html
// Storing images as Base64 is usually a very bad idea.
// Replace it on production website with other solutions:
// https://ckeditor.com/docs/ckeditor5/latest/features/images/image-upload/image-upload.html
// 'Base64UploadAdapter',
'RealTimeCollaborativeComments',
'RealTimeCollaborativeTrackChanges',
'RealTimeCollaborativeRevisionHistory',
'PresenceList',
'Comments',
'TrackChanges',
'TrackChangesData',
'RevisionHistory',
'Pagination',
'WProofreader',
// Careful, with the Mathtype plugin CKEditor will not load when loading this sample
// from a local file system (file://) - load this site via HTTP server if you enable MathType
'MathType'
]
});
}
</script>
 
<style>
.ck-editor__editable {
max-height: 400px;
background-color: white!important;
color: black!important;
}
</style>