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

@ -0,0 +1,94 @@
/*
* 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/>.
*/
import {Controller} from "@hotwired/stimulus";
import "tom-select/dist/css/tom-select.bootstrap5.css";
import '../../css/components/tom-select_extensions.css';
import TomSelect from "tom-select";
/**
* This is the frontend controller for StaticFileAutocompleteType form element.
* Basically it loads a text file from the given url (via data-url) and uses it as a source for the autocomplete.
* The file is just a list of strings, one per line, which will be used as the autocomplete options.
* Lines starting with # will be ignored.
*/
export default class extends Controller {
_tomSelect;
connect() {
let settings = {
persistent: false,
create: true,
maxItems: 1,
maxOptions: 100,
createOnBlur: true,
selectOnTab: true,
valueField: 'text',
searchField: 'text',
orderField: 'text',
//This a an ugly solution to disable the delimiter parsing of the TomSelect plugin
delimiter: 'VERY_L0NG_D€LIMITER_WHICH_WILL_NEVER_BE_ENCOUNTERED_IN_A_STRING'
};
if (this.element.dataset.url) {
const url = this.element.dataset.url;
settings.load = (query, callback) => {
const self = this;
if (self.loading > 1) {
callback();
return;
}
fetch(url)
.then(response => response.text())
.then(text => {
// Convert the text file to array
let lines = text.split("\n");
//Remove all lines beginning with #
lines = lines.filter(x => !x.startsWith("#"));
//Convert the array to an object, where each line is in the text field
lines = lines.map(x => {
return {text: x};
});
//Unset the load function to prevent endless recursion
self._tomSelect.settings.load = null;
callback(lines);
}).catch(() => {
callback();
});
};
}
this._tomSelect = new TomSelect(this.element, settings);
}
disconnect() {
super.disconnect();
//Destroy the TomSelect instance
this._tomSelect.destroy();
}
}

13268
public/kicad/footprints.txt Normal file

File diff suppressed because it is too large Load diff

12877
public/kicad/symbols.txt Normal file

File diff suppressed because it is too large Load diff

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']);
}
}