Added an autocomplete feature for Kicad symbols and footprints

This commit is contained in:
Jan Böhmer 2023-12-02 19:40:26 +01:00
parent 30b2c8b841
commit f28e369c01
8 changed files with 26369 additions and 4 deletions

View file

@ -66,8 +66,9 @@ class EDACategoryInfoType extends AbstractType
'class' => 'checkbox-inline'
]
])
->add('kicad_symbol', TextType::class, [
->add('kicad_symbol', KicadFieldAutocompleteType::class, [
'label' => 'eda_info.kicad_symbol',
'type' => KicadFieldAutocompleteType::TYPE_SYMBOL,
'attr' => [
'placeholder' => t('eda_info.kicad_symbol.placeholder'),
]

View file

@ -36,7 +36,8 @@ class EDAFootprintInfoType extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('kicad_footprint', TextType::class, [
->add('kicad_footprint', KicadFieldAutocompleteType::class, [
'type' => KicadFieldAutocompleteType::TYPE_FOOTPRINT,
'label' => 'eda_info.kicad_footprint',
'attr' => [
'placeholder' => t('eda_info.kicad_footprint.placeholder'),

View file

@ -71,14 +71,16 @@ class EDAPartInfoType extends AbstractType
'class' => 'checkbox-inline'
]
])
->add('kicad_symbol', TextType::class, [
->add('kicad_symbol', KicadFieldAutocompleteType::class, [
'label' => 'eda_info.kicad_symbol',
'type' => KicadFieldAutocompleteType::TYPE_SYMBOL,
'attr' => [
'placeholder' => t('eda_info.kicad_symbol.placeholder'),
]
])
->add('kicad_footprint', TextType::class, [
->add('kicad_footprint', KicadFieldAutocompleteType::class, [
'label' => 'eda_info.kicad_footprint',
'type' => KicadFieldAutocompleteType::TYPE_FOOTPRINT,
'attr' => [
'placeholder' => t('eda_info.kicad_footprint.placeholder'),
]

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Form\Part\EDA;
use App\Form\Type\StaticFileAutocompleteType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* This is a specialized version of the StaticFileAutocompleteType, which loads the different types of Kicad lists.
*/
class KicadFieldAutocompleteType extends AbstractType
{
public const TYPE_FOOTPRINT = 'footprint';
public const TYPE_SYMBOL = 'symbol';
public const FOOTPRINT_PATH = '/kicad/footprints.txt';
public const SYMBOL_PATH = '/kicad/symbols.txt';
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('type');
$resolver->setAllowedValues('type', [self::TYPE_SYMBOL, self::TYPE_FOOTPRINT]);
$resolver->setDefaults([
'file' => fn(Options $options) => match ($options['type']) {
self::TYPE_FOOTPRINT => self::FOOTPRINT_PATH,
self::TYPE_SYMBOL => self::SYMBOL_PATH,
}
]);
}
public function getParent(): string
{
return StaticFileAutocompleteType::class;
}
}

View file

@ -0,0 +1,63 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\Form\Type;
use Symfony\Component\Asset\Packages;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Implements a text type with autocomplete functionality based on a static file, containing a list of autocomplete
* suggestions.
* Other values are allowed, but the user can select from the list of suggestions.
* The file must be located in the public directory!
*/
class StaticFileAutocompleteType extends AbstractType
{
public function __construct(
private readonly Packages $assets
) {
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setRequired('file');
$resolver->setAllowedTypes('file', 'string');
}
public function getParent(): string
{
return TextType::class;
}
public function finishView(FormView $view, FormInterface $form, array $options): void
{
//Add the data-controller and data-url attributes to the form field
$view->vars['attr']['data-controller'] = 'elements--static-file-autocomplete';
$view->vars['attr']['data-url'] = $this->assets->getUrl($options['file']);
}
}